使用CoreText显示NSAttributedString [英] Display NSAttributedString using CoreText
问题描述
我听说我可以使用CoreText显示NSAttributedString,有人能说我怎么样(最简单的方式)?
I have heard that I can display a NSAttributedString using CoreText, can anyone say me how (The simplest way)?
请不要用CATextLayer回答或OHAttributedLabel。
Please, don't answer with CATextLayer or OHAttributedLabel.
我知道在这个论坛上有很多关于此的问题,但我找不到答案
I know that there are a lot of questions about this in this forum, but I haven't find the answer
谢谢!!
推荐答案
我认为最简单的方法(使用Core Text)是:
I think that the simplest way (using Core Text) is:
// Create the CTLine with the attributed string
CTLineRef line = CTLineCreateWithAttributedString(attrString);
// Set text position and draw the line into the graphics context called context
CGContextSetTextPosition(context, x, y);
CTLineDraw(line, context);
// Clean up
CFRelease(line);
如果您正在绘制大量文本,使用Framesetter会更有效但这是Apple推荐的方法如果您只需要显示少量文本(如标签)并且不需要您创建路径或框架(因为它是由 CTLineDraw
)。
Using a Framesetter is more efficient IF you are drawing lots of text but this is the method recommended by Apple if you just need to display a small amount of text (like a label) and doesn't require you to create a path or frame (since it is done for you automatically by CTLineDraw
).
这篇关于使用CoreText显示NSAttributedString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!