Custom Arabic font in iOS

Thursday Moh picture Thursday Moh · Sep 11, 2010 · Viewed 9.1k times · Source

I am trying to make an iphone application that displays mainly Arabic language content. I want to use a custom Arabic font for displaying that content, not the iPhone default Arabic font. I used the following code:

- (void)viewDidLoad {
    [super viewDidLoad];
    [arabicLabel setFont: [UIFont fontWithName: @"Simple Indust Shaded" size: 20]];
    arabicLabel.text = @"السلام عليكم";
}

where arabicLabel is an IBOutlet UILabel. I put the label in the App resources and add it to the info.plist file as UIAppFont array item. Also the font is Unicode.

When I run this code on the simulator, the label displayed the default Arabic font (not my custom font); however it set its size to 20 as stated in the code!

I don't understand what I did wrong here... I know that in the info.plist file I must add the font file name with extension (ttf), and I did that. Also, I know that in the previous code I had to put the actual font name not file name, so I opened the file in my Mac and used the title that appeared when the file is opened (Simple Indust Shaded).

Am I missing something? Are there any extra requirements for custom fonts other than being Unicode?

NB: I did the same procedure for custom English font, and it worked!...

Answer

Hejazi picture Hejazi · Dec 5, 2010

Short answer

This seems to be a bug in iOS.

Long answer

As you may know, the arabic text needs to be shaped before it can be rendered, that means converting the basic letters (Unicode:0600-06FF) to their proper corresponding contextual letters (Unicode:FE70-FEFF).

iOS does this preprocessing automatically for arabic text, but for some reason, it forgets about the custom font and just uses system default font when it renders the connected letters.

So, it's a bug in iOS and you should wait for it to be solved.

Workaround

Though, if you can't wait, you can use this workaround: Do the preprocessing of the text manually before rendering it. This will allow skipping iOS preprocessing phase which causes this issue and thus rendering the text using your custom font.

If you want to test this solution, just copy this following line and try it in your project:

arabicLabel.text = @"ﺍﻟﺴﻼﻡ ﻋﻠﻴﻜﻢ";