LoginSignup
5
4

More than 5 years have passed since last update.

CoreTextを使ってNSAttributedStringを描画する

Last updated at Posted at 2012-03-16
#import "NSAttributedString+DrawInRect.h"
#import <CoreText/CoreText.h>
@implementation NSAttributedString (DrawInRect)

-(void)drawInRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    CGContextTranslateCTM(ctx, 0.0f, rect.size.height + rect.origin.y * 2);
    CGContextScaleCTM(ctx, 1.0f, -1.0f);    
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self);
    CGPathRef path = [[UIBezierPath bezierPathWithRect:rect] CGPath];    
    CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
    CGPathRelease(path);
    CFRelease(framesetter);
    CTFrameDraw(textFrame, ctx);    
    CGContextRestoreGState(ctx);
}

@end

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4