【问题标题】:Core Text: Render to an Odd Shape核心文本:渲染成奇怪的形状
【发布时间】:2010-09-28 13:40:46
【问题描述】:

我正在尝试将文本呈现为不是矩形的形状。以下代码在我添加矩形路径时有效,但在添加椭圆路径时无效。最终,我想画到任何路径(L 形等),有没有人有这方面的运气?

-(void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);

    CGMutablePathRef path = CGPathCreateMutable();
    float w = 200.0;
    float h = 300.0;
    CGRect bounds = CGRectMake(10.0, 10.0, w, h);

    // I draw using only one of the following lines
    CGPathAddRect(path, NULL, bounds); // THIS LINE WILL WORK
    //CGPathAddEllipseInRect(path, NULL, bounds); // THIS LINE WILL CRASH IT


    // Initialize an attributed string.
    CFStringRef string = CFSTR("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
    CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0), string);

    // Create the framesetter with the attributed string.
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString); 
    CFRelease(attrString);

    // Create the frame and draw it into the graphics context
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

    // Flip the text
    CGAffineTransform flip = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0, self.frame.size.height);
    CGContextConcatCTM(context, flip);

    CTFrameDraw(frame, context);
    CFRelease(framesetter);
    CFRelease(frame);
}

任何帮助或想法将不胜感激。我被难住了。

【问题讨论】:

  • 有趣。您找到任何解决方案了吗?你可能想提交一个错误然后......
  • 尚无解决方案。我提交了错误 #8523733。 CTFramesetterCreateFrame 为任何非矩形路径返回 null(当我释放框架时发生崩溃)。根据文档,这应该没问题。如果我能得到任何解决方案,我会更新这篇文章。

标签: iphone ipad core-graphics core-text cgpath


【解决方案1】:

http://lists.apple.com/archives/quartz-dev/2010/Mar/msg00049.html

问题是文档错误。 该功能目前不支持非矩形路径。 我希望它会尽快实施,但现在还没有。

【讨论】:

  • 它现在支持非矩形路径。我认为改进来自 iOS 4.2,但我不确定。
【解决方案2】:

您可以使用任何类型的 UIBeizerPath,示例代码如下: https://github.com/erica/iOS-5-Cookbook/tree/master/C10/19-Drawing%20onto%20Paths

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 2015-09-05
    相关资源
    最近更新 更多