Arabic TTF (TrueType font) in UIWebView iOS 4.2.1
Hi
I finally figured it out. You can load almost all (.ttf) fonts, either arabic, english or whatever on iOS 3.2 above. You just need to use the correct text (in terms of unicode), to display it.
Custom Arabic Font is working in iOS7 but not in iOS6
Yes it didn't work in iOS6 and before, this seems it was a bug, there are some tricks you can do, check below SO questions:
Problem using custom Arabic font on iPhone
Custom Arabic font in iOS
Custom font in a storyboard?
Arabic text with custom fonts in iOS
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.
Porting Arabic font to iOS System
Fonts are complex programs that run on embedded hardware interpreters and font engines (the same way game ROMs run on game hardware or in emulators), and don't just contain the pictures for each letter, but also all the instructions on how to position, combine, and substitute those letters based on what sequence of input it's being told to process.
For almost every font, just copying the glyph outlines is not enough, you also need to make sure that:
- the original font's glyph ordering is preserved,
- the GPOS table gets copied over (which determines mark positioning, kerning, etc), and
- the GSUB table gets copied over (which handles glyph substitution, without which you can't even write Arabic. As an example, you need ـب, ـبـ, بـ or ب depending on context for the same "letter" bā’)
So you generally want to unpack the TTC
(which as of OpenType 1.7 can be either truetype or postscript/type2, so that's a thing to remember for the future), then perform the replacements of glyphs (with order-preservation), as well as replacing the GPOS and GSUB tables (using FontForge or TTX) and then pack it back up into the TTC
.
How to use Arabic Custom Fonts in objective c
Follow these steps
1.) Drag fonts into your project.
2.) Define your fonts name in your plist. Open plist > right click > add row > type : Fonts provided by application
> press + > yourFontName.ttf.
It look like this in info plist
Fonts provided by application : Array : (2 items)
item 0 : fontNameOne.ttf
item 1 : fontNameTwo.ttf
3.) Either you can check it from storyboard or you can do programmatically.
yourLabel.font=[UIFont fontWithName:@"yourFont" size:16.0f];//do not use .ttf here
Custom arabic font working in iOS 7 but not in iOS 6 or earlier
Problem solved...
UILabel *arTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 300, 100)];
arTitle.backgroundColor = [UIColor clearColor];
arTitle.textColor = [UIColor blueColor];
arTitle.textAlignment = NSTextAlignmentCenter;
arTitle.font = [UIFont fontWithName:@"GEDinarOne-Medium" size:16.0];
arTitle.numberOfLines = 0;
arTitle.text = @"واللقب هو عربي مع خط مخصص";
// these two line made effect...
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:arTitle.text attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"GEDinarOne-Medium" size:16], NSLigatureAttributeName: @2}];
arTitle.attributedText = attributedString;
Related Topics
CSS Inverted Trapezium When Width Will Differ
How to Make a Scrollable Element Fill 100% of the Remaining Height of a Parent Element
Syntax Highlighting in Jekyll Using Redcarpet
Access the Locale in Ruby in CSS Files
CSS: How to Align Vertically a "Label" and "Input" Inside a "Div"
Make HTML5 Video Poster Be Same Size as Video Itself
How to Write a Caption Under an Image
How to Make The Overflow CSS Property Work with Hidden as Value
How to Align a Div to The Top of Its Parent But Keeping Its Inline-Block Behaviour
How to Line Up 3 Divs on The Same Row
How to Get Floating Divs Inside Fixed-Width Div to Continue Horizontally
Set Div Height Equal to Screen Size
CSS Text Ellipsis When Using Variable Width Divs
How to Set Height Property for Span
Using Background-Attachment:Fixed in Safari on The Ipad
Why Can't I Make My Div 100% Height If I Use an HTML5 Doctype? How to Get It 100% Height