海吉同志们,
我目前正在尝试将一个字母和/或多个字母转换为 CGPathRef,以便手动将它们绘制到自定义 UIView 中。我尝试了 CoreText 和 Framesetters 的方法,包括这个小片段,但它似乎不起作用......
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
可以轻松地将自身绘制到自定义视图中)。
如果你真的想得到CGPath
,您需要一直钻取到CGGlyph
然后使用CTFontCreatePathForGlyph
。如果您只是想快速重绘文本,我可能会将其绘制成CGLayer
(not CALayer
) with CTFrameDraw
以供以后重复使用。
如果你还真的想要CGPath
对于字形,看看低级文本渲染。你需要的代码就在那里。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)