核心文字:渲染为奇怪的形状 [英] Core Text: Render to an Odd Shape

查看:114
本文介绍了核心文字:渲染为奇怪的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文本渲染为不是矩形的形状。添加矩形路径时,以下代码有效,但添加椭圆路径时则无效。最终,我想画任何路径(L形等),有没有人对此有任何好运?

I am trying to render text to a shape that isn't a rectangle. The following code works when I add a rectangular path but not when I add an elliptical path. Ultimately, I'd like to draw to any path (L shape, etc), has anyone had any luck with this?

-(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);
}

任何帮助或想法都将不胜感激。我很难过。

Any help or ideas would be greatly appreciated. I'm stumped.

推荐答案

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

问题是文档错误。
该函数当前不支持非矩形路径。
我希望它很快就会实施,但现在还没有。

The problem was documentation bug. The function does not support non-rectangular path currently. I hope it'll be implemented soon, but it is not yet.

这篇关于核心文字:渲染为奇怪的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆