Html – Arabic text with custom fonts in iOS

arabicfontshtmliosuiwebview

I'm working on an application that requires arabic text to be displayed, using custom arabic font. The problem is that i'm not having any luck in displaying the arabic text using custom arabic fonts. I've tried to display the text in UILabel, UITextField, UITextView, UIWebView, FontLabel (by Zynga), and using CoreText.

You can find sample projects here:

Sample application using UILabel, UITextField, UITextView and CoreText

Sample application using UIWebView (HTML)

Sample application using UIWebView (HTML): You'll have to install the
fonts in the sample project. You can then compare the results and see
the problem by running the application in the simulator (or iPad), and
opening Sample.html file in the browser (Safari).

I've posted some details on Apple's developer forum but i've always found stackoverflow to be much more active then Apple's official developer forum. Here's a link to that post:

Using custom arabic font in iOS

What am i doing wrong?

Note: The fonts are loaded into the system correctly, and i can successfully apply custom fonts to regular (english) text.

p.s. There are a couple of posts on stackoverflow that talk about this subject but none have been of much help, so i'm posting a new question.

Best Answer

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.