Core Text: Render to an Odd Shape
Asked Answered
T

2

4

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.

Trotman answered 28/9, 2010 at 13:40 Comment(2)
Interesting. Have you found any solution? You might want to file a bug then...Revulsion
No solution yet. I filed bug #8523733. CTFramesetterCreateFrame returns null for any non-rectangular path (the crash happens when I release the frame). According to the docs, this should be fine. I'll update this post if I can get any resolution.Trotman
S
4

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.

Snowplow answered 29/10, 2010 at 6:18 Comment(1)
It does now support non-rectangular paths. I think the improvement came in iOS 4.2, but I'm not sure.Multitudinous
C
0

you can draw on any sort of UIBeizerPath, sample code here: https://github.com/erica/iOS-5-Cookbook/tree/master/C10/19-Drawing%20onto%20Paths

Chase answered 2/4, 2012 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.