import "CoreText/CoreText.h"
-
(void)drawRect:(CGRect)rect {
[super drawRect:rect];
// 步骤 1
CGContextRef context = UIGraphicsGetCurrentContext();// 步骤 2
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);// 步骤 3
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds);// 步骤 4
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:10];
UIColor *color = COLOR_WHITE;
NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.string attributes:
@{NSForegroundColorAttributeName:color,
NSParagraphStyleAttributeName: paragraphStyle}];
CTFramesetterRef framesetter =
CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);
CTFrameRef frame =
CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, [string length]), path, NULL);// 步骤 5
CTFrameDraw(frame, context);// 步骤 6
CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);
}