iOS 中带有自定义字体的阿拉伯文本
我正在开发一个需要使用自定义阿拉伯字体显示阿拉伯文本的应用程序。问题是我没有运气使用自定义阿拉伯字体显示阿拉伯文本。我尝试在 UILabel、UITextField、UITextView、UIWebView、FontLabel(Zynga 提供)中显示文本,并使用 CoreText。
您可以在此处找到示例项目:
使用 UILabel、UITextField、UITextView 和 CoreText 的示例应用程序
使用 UIWebView (HTML) 的示例应用程序:您必须安装 示例项目中的字体。然后您可以比较结果并查看 通过在模拟器(或 iPad)中运行应用程序来解决问题,以及 在浏览器 (Safari) 中打开 Sample.html 文件。
我已经在 Apple 开发者论坛上发布了一些详细信息,但我总是发现 stackoverflow 比 Apple 官方开发者论坛活跃得多。这是该帖子的链接:
我做错了什么?
注意:字体已正确加载到系统中,并且我可以成功地将自定义字体应用于常规(英文)文本。
ps stackoverflow 上有几篇文章讨论了这个主题,但没有一个有太大帮助,所以我发布了一个新问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更新:
从 iOS 7 开始,您实际上不需要使用 Core Text 来渲染自定义阿拉伯字体。您可以将
UILabel
和/或UITextView
与NSAttributedString
一起使用。结果与使用 Core-Text 得到的结果相同。但是,根据您的要求,使用 Core Text 仍然是更好的选择。更新:
我已将此作为错误报告给 Apple,但我不确定他们何时会添加对阿拉伯字体的支持。目前,还没有简单的方法可以做到这一点。我最终使用了默认的系统字体,这不是很好。
原始消息
我确实设法构建了一个使用自定义阿拉伯字体的古兰经应用程序。我使用已知的阿拉伯字体和
Core Text
框架来获得所需的结果。你可以通过检查应用程序古兰经来看到我最终得到的结果iPad 版 Presenter,可从 App Store 获取。这里有一些示例代码可以帮助您:
请注意,我使用的是
CATextLayer
和CTFontRef
。这种方法存在一些问题。1.您将不得不忍受所选“自定义阿拉伯字体”中的问题。
2. 您必须使用使用字体支持的扩展字符的阿拉伯文本。
HTH。
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.
我第一次学习 iOS 开发是在 2012 年中,当时我一直在互联网上到处寻找 iPhone 中自定义波斯字体的合适解决方案,但始终找不到合适的解决方案。我尝试过一个
Core Text
库,但它并不总是正常工作,特别是您必须创建一个额外的subview
来添加带有自定义字体的文本在其中。我不久前尝试过阿拉伯语转换器,它工作得很好,但是使用新的 Xcode,除了 Web 视图之外,它根本没有对文本进行任何更改,而且它只是出现了故障!所以……无论如何,我很高兴苹果正在解决支持更多语言的问题。现在,这是一个非常简单的解决方案,效果非常好:
info.plist
文件,添加行
,应用程序提供的字体< /code> 在新行中,
UITextView *波斯语文本;
persianText.text = @"德瑞·巴德·德·德·德·德·德·德·德·德·德·德·德·德·德·德·德·德·德·德";
persianText.font = [UIFont fontWithName:@"IranNastaliq" size: 12.0];
安...!构建并运行...Tadaaa!有用!
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!
这些课程将完成这项工作:
https:// github.com/Accorpa/Arabic-Converter-From-and-To-Arabic-Presentation-Forms-B
下载它们,我很确定这回答了你的问题,因为我有完全相同的之前的问题!
代码示例:
现在 infoText 将正确显示字体:)
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 :)
如果您的目标是 iOS 6,则可以使用
NSAttributedString
更简单地实现此目的:请参阅我的答案 此处有关与阿拉伯字体通常相关的其他问题(例如行距和连字)的一些讨论。
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.
对于UIWebview:基本上接受的答案没有解决
UIWebView
案例.. 这是我解决这个问题的方法。首先:使用hpple,一个obj-c HTML解析器,我经历了html 并基本上废弃了文本并将其附加到
NSMutableString
中,如下所示:然后,我按照 本教程。以下是其他文件:
CTContainerView.h
CTContainerView.m
CTContentView.h
CTContentView.m
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