2011-09-16 26 views
2

HEJ研究員,從文本獲取CGPath

我目前正在試圖將文字和/或他們的多重轉換爲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); 

回答

6

CTFrameGetPath返回路徑的外接CTFrame,不按圖紙框架生成的路徑。我假設你正在執行上述操作,因爲您正在緩存路徑以改善以後的繪圖性能? (由於CTFrame可以很容易地將自己繪製成自定義視圖)。

如果您確實想獲得CGPath,則需要一直鑽到CGGlyph,然後使用CTFontCreatePathForGlyph。如果你只是想快速重繪文本,我可能會把它畫成CGLayer(而不是CALayer)與CTFrameDraw以備後用。

如果你仍然真的想要CGPath爲字形,看看Low-level text rendering。你需要的代碼就在那裏。