iOS中使用自定义字体的阿拉伯文字

Mus*_*afa 6 html fonts arabic uiwebview ios

我正在开发一个应用程序,需要使用自定义阿拉伯语字体显示阿拉伯语文本.问题是,我没有运气使用自定义阿拉伯字体显示阿拉伯语文本.我试图在UILabel,UITextField,UITextView,UIWebView,FontLabel(由Zynga)和使用CoreText显示文本.

您可以在此处找到示例项目:

使用UILabel,UITextField,UITextView和CoreText的示例应用程序

使用UIWebView(HTML)的示例应用程序

使用UIWebView(HTML)的示例应用程序:您必须在示例项目中安装字体.然后,您可以通过在模拟器(或iPad)中运行应用程序并在浏览器(Safari)中打开Sample.html文件来比较结果并查看问题.

我在Apple的开发者论坛上发布了一些细节,但我总是发现stackoverflow比Apple的官方开发者论坛更加活跃.这是该帖子的链接:

在iOS中使用自定义阿拉伯字体

__PRE__

注意:字体正确加载到系统中,我可以成功将自定义字体应用于常规(英语)文本.

ps stackoverflow上有几篇帖子谈论这个主题,但没有一个有很大的帮助,所以我发布了一个新的问题.

Mus*_*afa 8

更新:

从iOS 7开始,您不需要使用Core Text来呈现自定义的阿拉伯字体.您可以使用UILabel和/或UITextView使用NSAttributedString.结果与使用Core-Text的结果相同.但是,根据您的要求,使用Core Text仍然是更好的选择.

更新:

我已将此报告为Apple的错误,但我不确定他们何时会添加对阿拉伯字体的支持.目前,没有简单的方法可以做到这一点.我最终使用默认的系统字体,这不是很好.

原始信息

我确实设法构建了一个使用自定义阿拉伯字体的古兰经应用程序.我使用已知的阿拉伯字体与Core Text框架来获得所需的结果.您可以通过检查应用程序商店中提供的应用程序Quran Presenter for iPad来查看最终得到的结果.

这里有一些示例代码可以帮助您:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

请注意,我正在使用CATextLayerCTFontRef.这种方法存在一些问题.1.您将不得不忍受选定的"自定义阿拉伯字体"中的问题.2.您必须使用使用字体支持的扩展字符的阿拉伯语文本.

HTH.