iphone - sizeWithFont : method 的替代方案

标签 iphone cocoa-touch uikit core-graphics

我有一个类,其工作是将文本解析为多个页面。 我使用 sizeWithFont: 方法来识别一页何时结束而另一页何时开始。 但不幸的是,由于需要解析的文本量相当大,整个操作需要在后台线程上执行(需要几秒钟才能完成)。因此,有时我的界面上会出现视觉伪影(UIKit 不是线程安全的,但我同时从多个线程调用它),我很想摆脱它。

我需要摆脱在后台线程上使用 sizeWithFont: 。但这种方法似乎没有其他选择。使用 Core Graphics 找出文本宽度的唯一方法是使用苹果文档中所述的方法:

  1. Call the function CGContextGetTextPosition to obtain the current text position.
  2. Set the text drawing mode to kCGTextInvisible using the function CGContextSetTextDrawingMode.
  3. Draw the text by calling the function CGContextShowText to draw the text at the current text position.
  4. Determine the final text position by calling the function CGContextGetTextPosition.
  5. Subtract the starting position from the ending position to determine the width of the text.

但我真的很担心这会导致巨大的性能损失。

有人知道另一种找出文本宽度的方法吗?

最佳答案

如果您有可能将您的应用程序定位为 iPhone OS 3.2,您可以查看 Core Text .

尽管如此,请注意整个 Core Text 库也不是完全线程安全的。来自文档:

Multicore Considerations: All individual functions in Core Text are thread safe. Font objects (CTFont, CTFontDescriptor, and associated objects) can be used by simultaneously by multiple operations, work queues, or threads. However, the layout objects (CTTypesetter, CTFramesetter, CTRun, CTLine, CTFrame, and associated objects) should be used in a single operation, work queue, or thread.

关于iphone - sizeWithFont : method 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2371402/

相关文章:

android - 适用于 Android/iphone/ipad 的开源或其他 pgp/gpg 库

iphone - UIView backgroundColor与图像

objective-c - 从动态实例化的类(objective-c)设置主视图 Controller 中的属性值

ios - Storyboard中具有不同单元格大小和单元格内容的 UITableViewController

iphone - 获取 UITableView 部分的框架

iphone - 访问应用程序visibleViewController?

ios - 将数据导出/共享到另一个应用程序安装

ios - 在模拟器上旋转 iPhone 时出现 UICollectionViewCell 大小调整问题

iphone - 下载普通图像与视网膜设备图像 (2x)

ios - 当 UITextView 的内容更多时,文本没有出现?