Iphone 从文本获取CGPath
Hej fellows 我目前正在尝试将一个字母和/或多个字母转换为CGPathRef,以便手动将它们绘制到自定义UIView中。我尝试了CoreText和framesetter的方法,包括这个小片段,但它似乎不起作用Iphone 从文本获取CGPath,iphone,cgpath,core-text,Iphone,Cgpath,Core Text,Hej fellows 我目前正在尝试将一个字母和/或多个字母转换为CGPathRef,以便手动将它们绘制到自定义UIView中。我尝试了CoreText和framesetter的方法,包括这个小片段,但它似乎不起作用 NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:content
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
,则需要深入到cglyph
,然后使用CTFontCreatePathForGlyph
。如果您只是想快速重画文本,我可能会使用CTFrameDraw
将其绘制到CGLayer
(而不是CALayer
)中,以便以后重用
如果您仍然希望glyph使用CGPath
,请查看。你需要的代码就在那里