从Text获取CGPath

Tim*_*cht 2 iphone cgpath core-text

Hej研究员,

我正在尝试将一个字母和/或多个字母转换为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);
Run Code Online (Sandbox Code Playgroud)

Rob*_*ier 6

CTFrameGetPath返回包围的路径CTFrame,而不是绘制框架生成的路径.我假设您正在执行上述操作,因为您正在缓存路径以改善以后的绘图性能?(因为CTFrame可以很容易地将自己绘制成自定义视图).

如果你真的想要获得CGPath,你需要一直钻到CGGlyph然后使用CTFontCreatePathForGlyph.如果您只是想快速重绘文本,我可能会将其绘制成CGLayer(不CALayer)以CTFrameDraw供以后重用.

如果你仍然真的想要CGPath用于字形,请看一下低级文本渲染.你需要的代码在那里.