One Quickie


Drawing wrapped text in CoreText (Core Text->General)
Say you have a string, and you just want to draw it pretty plain, but wrapped in a rectangle. CoreText can do that for you:
- (void) drawText: (NSString *) text  inRect: (CGRect) rect {

    CTFontRef font = CTFontCreateWithName (CFSTR("Helvetica"), 15.0, NULL);
    NSDictionary *attributes =
        @{ (__bridge id)kCTFontAttributeName : (__bridge id) font };

    CFAttributedStringRef attrString =
        CFAttributedStringCreate (kCFAllocatorDefault,
                                  (__bridge CFStringRef) text,
                                  (__bridge CFDictionaryRef) attributes);

    CTFramesetterRef fsetter = CTFramesetterCreateWithAttributedString (attrString);
    CGPathRef path = CGPathCreateWithRect (rect, NULL);

    CTFrameRef frame = CTFramesetterCreateFrame (fsetter,
                                      CFRangeMake (0, 0),
                                      path, NULL);

    CGContextRef context = ...;
    CGContextSetTextMatrix (context, CGAffineTransformIdentity);

    CTFrameDraw (frame, context);

     CFRelease (font);
     CFRelease (attrString);
     CFRelease (fsetter);
     CGPathRelease (path);

} // drawTextInRect



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com