我听说我可以使用 CoreText 显示 NSAttributedString,谁能告诉我怎么做(最简单的方法)?
请不要用 CATextLayer 或 OHAttributedLabel 回答。
我知道这个论坛有很多关于这个的问题,但是我没有找到答案
谢谢!!
最佳答案
最简单的方法?像这样:
CGContextRef context = UIGraphicsGetCurrentContext();
// Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Create a path to render text in
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds );
// An attributed string containing the text to render
NSAttributedString* attString = [[NSAttributedString alloc]
initWithString:...];
// create the framesetter and render text
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(0, [attString length]), path, NULL);
CTFrameDraw(frame, context);
// Clean up
CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);
关于iphone - 使用 CoreText 显示 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8322020/