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.
These classes will do the job :
https://github.com/Accorpa/Arabic-Converter-From-and-To-Arabic-Presentation-Forms-B
Download them, and I'm pretty sure that answers your question because I had the exact same problem before!
An example of the code :
Now infoText will display the font correctly :)
I first started learning iOS development back in mid 2012, and I've been searching everywhere on the Internet to find a proper solution for custom Persian fonts in iPhone, and could never come up with a proper solution. There was a
Core Text
library I tried once, and it didn't work always fine, specially that you have to make an additionalsubview
to add your text with the custom font in it. I tried theArabic Converter
a while back and it worked alright, but with the new Xcode it wasn't making a change to the text at all, except in web views, and it only malfunctioned!So... Anyways, I'm glad Apple is working out problems to support more languages. Now here's the very simple solution that works perfectly fine:
info.plist
file,Add row
,Fonts provided by application
in the new row,UITextView *persianText; persianText.text = @"در جستجوی قهرمان نباشید \nبلکه خود قهرمان شوید"; persianText.font = [UIFont fontWithName:@"IranNastaliq" size: 12.0];
Annnd...! Build and run... Tadaaa! It works!
Update:
As of iOS 7, you don't really need to use Core Text to render custom arabic font. You can use
UILabel
and/orUITextView
withNSAttributedString
. 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:
Note that I'm using
CATextLayer
andCTFontRef
. 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.
If you're targeting iOS 6, you can use
NSAttributedString
to achieve this a lot more simply:Refer to my answer here for some discussions on other matters commonly relevant to arabic fonts such as line-spacing and ligatures.
For UIWebview: basically the accepted answer doesn't address the
UIWebView
case.. So here is a way I addressed this problem.first: Using hpple, an obj-c HTML parser, I went through the html and basically scrapped the text and appended it to an
NSMutableString
like so:Then I used Core Text to render the text into a webview following the steps in this tutorial. Here are the other files:
CTContainerView.h
CTContainerView.m
CTContentView.h
CTContentView.m