html – iOS中包含自定义字体的阿拉伯文字

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

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

Sample application using UILabel,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).

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

Using custom arabic font in iOS

What am i doing wrong?

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

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

解决方法

更新:

从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,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];
}

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

HTH.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 <div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些