Update:
As of iOS 7, you don't really need to use Core Text to render custom arabic font. You can use UILabel
and/or UITextView
with NSAttributedString
. The results are the same as you get using Core-Text. However, depending on your requirements, using Core Text can still be a better option.
Update:
I've reported this as a bug to Apple, but i'm not sure when they'll add support for Arabic fonts. Currently, there's no easy way to do it. I ended up using the default system font, which is not very good.
Original Message
I did managed to build a Quran application that uses custom arabic font. I used known arabic font(s) with Core Text
framework to get the desired results. You can see the results I got in the end by checking the application Quran Presenter for iPad, which is available on the App Store.
Here's some sample code to help you out:
- (CTFontRef)newCustomFontWithName:(NSString *)aFontName
ofType:(NSString *)type
attributes:(NSDictionary *)attributes {
NSString *fontPath = [[NSBundle mainBundle] pathForResource:aFontName ofType:type];
NSData *data = [[NSData alloc] initWithContentsOfFile:fontPath];
CGDataProviderRef fontProvider = CGDataProviderCreateWithCFData((CFDataRef)data);
[data release];
CGFontRef cgFont = CGFontCreateWithDataProvider(fontProvider);
CGDataProviderRelease(fontProvider);
CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attributes);
CTFontRef font = CTFontCreateWithGraphicsFont(cgFont, 0, NULL, fontDescriptor);
CFRelease(fontDescriptor);
CGFontRelease(cgFont);
return font;
}
- (CATextLayer *)customCATextLayer {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:24.f], (NSString *)kCTFontSizeAttribute,
[NSNumber numberWithInt:1], (NSString *)kCTLigatureAttributeName,
nil];
CTFontRef font = [self newCustomFontWithName:@"PDMS_Saleem_QuranFont-signed"
ofType:@"ttf"
attributes:attributes];
CATextLayer *normalTextLayer = [[CATextLayer alloc] init];
normalTextLayer.font = font;
normalTextLayer.string = NSLocalizedString(@"Sample", nil);
normalTextLayer.wrapped = YES;
normalTextLayer.foregroundColor = [[UIColor blackColor] CGColor];
normalTextLayer.fontSize = 24.f;
normalTextLayer.alignmentMode = kCAAlignmentCenter;
normalTextLayer.frame = CGRectMake(0.f, 10.f, 320.f, 32.f);
CFRelease(font);
return [normalTextLayer autorelease];
}
- (void)viewDidLoad {
[super viewDidLoad];
CATextLayer *normalTextLayer = [self customCATextLayer];
[self.customView.layer addSublayer:normalTextLayer];
}
Note that I'm using CATextLayer
and CTFontRef
. There are a few problems with this approach.
1. You'll have to live with the issues in the selected "custom arabic font".
2. You'll have to use the arabic text that uses the extended characters supported by the font.
HTH.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…