sizeWithFont的替代方法: [英] Alternative for sizeWithFont: method

查看:68
本文介绍了sizeWithFont的替代方法:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a class which work is to parse a text into several pages. I use sizeWithFont: method to identify when one page ends and another starts. But unfortunately since the amount of text which needs to be parsed is pretty big, whole operation needs to be performed on a background thread (takes several second to complete). And therefore sometimes I get visual artifacts on my interface (UIKit is not thread safe but I call it from several threads simultaneously), which I would love to get rid of.

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

I need to get rid of using sizeWithFont: on background thread. But there just doesn't seem to be an alternative for this method. The only way to find out the width of the text with Core Graphics is to use method stated in apple's documentation:



  1. 调用函数CGContextGetTextPosition获取当前文本位置。

  2. 使用函数CGContextSetTextDrawingMode将文本绘制模式设置为kCGTextInvisible。

  3. 绘制文本通过调用CGContextShowText函数在当前文本位置绘制文本。

  4. 通过调用CGContextGetTextPosition函数确定最终文本位置。

  5. 减去

  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.


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

But I'm really concerned that this will lead to a huge performance loss.

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

Anyone knows another way to find out text width?

推荐答案

如果可以针对iPhone OS 3.2定位应用程序,则可以看看 Core Text

If you have the possibility to target your application for iPhone OS 3.2, you can take a look at Core Text.

不过,请注意,整个Core Text库不是也不完全是线程安全的。从文档中:

Although, note that the entire Core Text library isn't completely thread safe either. From the documentation:


多核注意事项:Core Text中的所有单个函数都是线程安全的。字体对象(CTFont,CTFontDescriptor和关联的对象)可以由多个操作,工作队列或线程同时使用。但是,应在单个操作,工作队列或线程中使用布局对象(CTTypesetter,CTFramesetter,CTRun,CTLine,CTFrame和关联的对象)。

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.

这篇关于sizeWithFont的替代方法:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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