我有一个在矩形中绘制 NSAttributedString 的方法:
-(void)drawInRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self);
// left column form
CGMutablePathRef leftColumnPath = CGPathCreateMutable();
CGPathAddRect(leftColumnPath, NULL, CGRectMake(rect.origin.x, -rect.origin.y, rect.size.width, rect.size.height));
// left column frame
CGFloat translateAmount = rect.size.height;
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, translateAmount);
CGContextScaleCTM(context, 1.0, -1.0);
CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), leftColumnPath, NULL);
CTFrameDraw(leftFrame, context);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, translateAmount);
CGContextScaleCTM(context, 1.0, -1.0);
CFRelease(leftFrame);
CFRelease(framesetter);
CGPathRelease(leftColumnPath);
}
几个月前,我在教程的帮助下把它放在一起。事实证明,默认情况下,这会绘制左对齐的字符串。我对核心文本不太狡猾,有谁知道我如何用文本对齐中心来绘制这个?
(请不要推荐外部的标签绘图类,我需要使用 Core Text 来做)。