Iphone 使用CoreText显示NSAttribute字符串
我听说我可以使用CoreText显示NSAttributedString,有人能告诉我怎么做吗(最简单的方法) 请不要用CATextLayer或OHAttributedLabel回答 我知道在这个论坛上有很多关于这个的问题,但是我还没有找到答案Iphone 使用CoreText显示NSAttribute字符串,iphone,ios,core-text,nsattributedstring,Iphone,Ios,Core Text,Nsattributedstring,我听说我可以使用CoreText显示NSAttributedString,有人能告诉我怎么做吗(最简单的方法) 请不要用CATextLayer或OHAttributedLabel回答 我知道在这个论坛上有很多关于这个的问题,但是我还没有找到答案 谢谢 最简单的方法?大概是这样的: CGContextRef context = UIGraphicsGetCurrentContext(); // Flip the coordinate system CGContextSetTextMatrix(c
谢谢 最简单的方法?大概是这样的:
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);
我认为最简单的方法(使用核心文本)是:
如果您正在绘制大量文本,使用框架设置器会更有效,但如果您只需要显示少量文本(如标签),并且不需要创建路径或框架(因为这是由
CTLineDraw
自动完成的),则这是Apple推荐的方法.从ios 6开始,您可以执行以下操作:
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineSpacing:40];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [labelText length])];
cell.label.attributedText = attributedString ;
如果你不想使用这些包装,看看里面,它们是如何工作的。你对此有何看法?
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineSpacing:40];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [labelText length])];
cell.label.attributedText = attributedString ;