在iOS中使用自定义字体的阿拉伯文字 [英] Arabic text with custom fonts in iOS

查看:268
本文介绍了在iOS中使用自定义字体的阿拉伯文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义阿拉伯字体来处理需要显示阿拉伯文字的应用程序。问题是,我没有运用自定义阿拉伯字体显示阿拉伯文字。我试图在UILabel,UITextField,UITextView,UIWebView,FontLabel(由Zynga)和使用CoreText显示文本。

你可以在这里找到示例项目:


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


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


我在苹果的开发者论坛上发布了一些细节,但是我总是发现stackoverflow要比苹果的官方开发者论坛活跃得多。以下是该帖子的链接:



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

 我做错了什么? 

注意:字体正确加载到系统中,并且我可以成功地将自定义字体应用于常规英文)text。



ps在stackoverflow上有几篇关于这个主题的文章,但是没有太多的帮助,所以我发布了一个新的问题。

解决方案

div>

更新:

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



更新

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



原始信息



我确实设法构建了一个使用自定义阿拉伯字体的古兰经应用程序。我用核心文本框架使用已知的阿拉伯文字体来获得所需的结果。您可以通过检查应用程序来查看我得到的结果古兰经演示者的iPad ,这是在App Store上。



下面是一些示例代码来帮助你:

   - (CTFontRef)newCustomFontWithName:(NSString *)aFontName $ b $ ofType:(NSString *)type 
属性:(NSDictionary *)attributes {
NSString * fontPath = [[NSBundle mainBundle] pathForResource:aFontName ofType:type];

NSData * data = [[NSData alloc] initWithContentsOfFile:fontPath];
CGDataProviderRef fontProvider = CGDataProviderCreateWithCFData((CFDataRef)data);
[数据发布];

CGFontRef cgFont = CGFontCreateWithDataProvider(fontProvider);
CGDataProviderRelease(fontProvider);

CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)attributes);
CTFontRef font = CTFontCreateWithGraphicsFont(cgFont,0,NULL,fontDescriptor);
CFRelease(fontDescriptor);
CGFontRelease(cgFont);
返回字体;

$(b

$($)$ b $(
$($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$' ,
[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];

$ / code>

请注意,我正在使用 CATextLayer CTFontRef 。这种方法有几个问题。
1.您将不得不使用所选的自定义阿拉伯字体处理问题。
2.您必须使用使用字体支持的扩展字符的阿拉伯文本。



HTH。


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.

解决方案

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.

这篇关于在iOS中使用自定义字体的阿拉伯文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆