iphone - 使用 CoreText 显示 NSAttributedString

标签 iphone ios core-text nsattributedstring

我听说我可以使用 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/

相关文章:

iphone - CoreText 页面 View 示例 (CTPageViewer)

ios - NSMutableAttributedString 是否自动连接相同的相邻属性?

ios - 默认 UIViewController 呈现动画的精确缓动和计时

ios - 如何使用 Swift 3 将单例与 Alamofire 一起使用?

ios - 如何在界面生成器中隐藏 UIPickerView?

ios - (NSCFType set) - iOS 6 中无法识别的选择器

iphone - 如何删除 iPhone 中的本地通知

ios - 在您的证书错误上找不到 iPhone 开发者/分发身份

objective-c - Obj-C... "Incompatible types in initialization"错误

iphone - iOS7屏幕截图有白噪声