从Text获取CGPath [英] Get CGPath from Text
问题描述
Hej研究员,
我正在尝试将一个字母和/或多个字母转换为CGPathRef,以便手动将它们绘制到自定义UIView中。我尝试过CoreText和Framesetters,包括这个小片段,但它似乎不起作用....
I'm currently trying to convert a letter and/or multiple of them to a CGPathRef for manually drawing them into a custom UIView. I tried the way over CoreText and Framesetters including this little snippet but it doesn't seem to work....
NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:content
attributes:nil];
// now for the actual drawing
CGContextRef context = UIGraphicsGetCurrentContext();
// flip the coordinate system
// draw
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)stringToDraw);
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), NULL, NULL);
return CTFrameGetPath(frame);
推荐答案
CTFrameGetPath
返回限定 CTFrame
的路径,而不是绘制框架生成的路径。我假设您正在执行上述操作,因为您正在缓存路径以改善以后的绘图性能? (因为 CTFrame
可以很容易地将自己绘制到自定义视图中。)
CTFrameGetPath
returns the path the circumscribes the CTFrame
, not the path generated by drawing the frame. I assume that you're doing the above because you are caching the paths to improve later drawing performance? (Since CTFrame
can easily draw itself into a custom view).
如果你真的想要获得 CGPath
,你需要一直钻到 CGGlyph
,然后使用 CTFontCreatePathForGlyph
。如果你只是想快速重绘文字,我可能会把它画成 CGLayer
(不是 CALayer
) code> CTFrameDraw 供以后重复使用。
If you really want to get CGPath
, you need to drill all the way down to the CGGlyph
and then use CTFontCreatePathForGlyph
. If you just want to quickly redraw text, I'd probably draw this into a CGLayer
(not CALayer
) with CTFrameDraw
for later reuse.
如果你真的想要 CGPath
对于字形,请查看低级文本呈现一>。您需要的代码就在那里。
If you still really want the CGPath
for the glyphs, take a look at Low-level text rendering. The code you need is in there.
这篇关于从Text获取CGPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!