NSAttributedString详解

来源:互联网 发布:数据文员主要做什么的 编辑:程序博客网 时间:2024/05/20 00:14
    转自: http://www.cnblogs.com/whyandinside/archive/2013/12/27/3493475.html
    转自:http://blog.csdn.net/zhangao0086/article/details/7616385

    An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string.

    这句话就是对这个类的一个最简明扼要的概括。NSAttributedString管理一个字符串,以及与该字符串中的单个字符或某些范围的字符串相关的属性。比如这个字符串“北京天安门”,“我”跟其他字符的颜色不一样,而“北京”与其他的字体和大小不一样,等等。NSAttributedString就是用来存储这些信息的,具体实现时,NSAttributedString维护了一个NSString,用来保存最原始的字符串,另有一个NSDictionary用来保存各个子串/字符的属性。

     

    源文档 <http://www.cnblogs.com/whyandinside/archive/2013/12/27/3493475.html>

     

     

    NSAttributedString可以让我们使一个字符串显示的多样化,但是目前到iOS 5为止,好像对它支持的不是很好,因为显示起来不太方便(至少没有在OS X上方便)。

    首先导入CoreText.framework,并在需要使用的文件中导入:

    #import<CoreText/CoreText.h>

    创建一个NSMutableAttributedString:

    1. NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is test!"]   
    1.                                               autorelease];  


    非常常规的创建方式,接下来我们给它配置属性:

    1. //把this的字体颜色变为红色  
    1. [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName  
    2.                     value:(id)[UIColor redColor].CGColor   
    1.                     range:NSMakeRange(0, 4)];  
    1. //把is变为黄色  
    1. [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName  
    2.                     value:(id)[UIColor yellowColor].CGColor   
    3.                     range:NSMakeRange(5, 2)];  
    1. //改变this的字体,value必须是一个CTFontRef  
    1. [attriString addAttribute:(NSString *)kCTFontAttributeName  
    1.                     value:(id)CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName,  
    1.                                                    14,   
    1.                                                    NULL)  
    1.                     range:NSMakeRange(0, 4)];  
    1. //给this加上下划线,value可以在指定的枚举中选择  
    1. [attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName  
    2.                     value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble]  
    3.                     range:NSMakeRange(0, 4)];  
    1. return attriString;  

    这样就算是配置好了,但是我们可以发现NSAttributedString继承于NSObject,并且不支持任何draw的方法,那我们就只能自己draw了。写一个UIView的子类(假设命名为TView),在initWithFrame中把背景色设为透明(self.backgroundColor = [UIColor clearColor]),然后在重写drawRect方法:

    1. -(void)drawRect:(CGRect)rect{  
    2.     [super drawRect:rect];  
    1.       
    1.     NSAttributedString *attriString = getAttributedString();  
    1.       
    1.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
    1.     CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));  
    1.       
    2.     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString);  
    1.     CGMutablePathRef path = CGPathCreateMutable();  
    1.     CGPathAddRect(path, NULL, rect);  
    1.       
    1.     CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);  
    1.     CFRelease(path);  
    1.     CFRelease(framesetter);  
    1.       
    1.     CTFrameDraw(frame, ctx);  
    1.     CFRelease(frame);  
    2. }  

    在代码中我们调整了CTM(current transformation matrix),这是因为Quartz 2D的坐标系统不同,比如(10, 10)到(20, 20)的直线坐标:

     

    坐标类似于数学中的坐标,可以先不调整CTM,看它是什么样子的,下面两种调整方法是完全一样的:

    1. CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));  


    ==

    1. CGContextTranslateCTM(ctx, 0, rect.size.height);  
    2. CGContextScaleCTM(ctx, 1, -1);  

    CTFramesetter是CTFrame的创建工厂,NSAttributedString需要通过CTFrame绘制到界面上,得到CTFramesetter后,创建path(绘制路径),然后得到CTFrame,最后通过CTFrameDraw方法绘制到界面上。

    如果想要计算NSAttributedString所要的size,就需要用到这个API:

    CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行时会算不准的,因为在CoreText里,行间距也是你来控制的。

    设置行间距和换行模式都是设置一个属性:kCTParagraphStyleAttributeName,这个属性里面又分为很多子

    属性,其中就包括

    • kCTLineBreakByCharWrapping
    • kCTParagraphStyleSpecifierLineSpacingAdjustment

    设置如下:

    1. //段落  
    1.     //line break  
    2. CTParagraphStyleSetting lineBreakMode;  
    3. CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping; //换行模式  
    4. lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode;  
    5. lineBreakMode.value = &lineBreak;  
    6. lineBreakMode.valueSize = sizeof(CTLineBreakMode);  
    7.     //行间距  
    8. CTParagraphStyleSetting LineSpacing;  
    9. CGFloat spacing = 4.0;  //指定间距  
    10. LineSpacing.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;  
    11. LineSpacing.value = &spacing;  
    12. LineSpacing.valueSize = sizeof(CGFloat);  
    13.   
    14. CTParagraphStyleSetting settings[] = {lineBreakMode,LineSpacing};  
    15. CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, 2);   //第二个参数为settings的长度  
    16. [attributedString addAttribute:(NSString *)kCTParagraphStyleAttributeName  
    17.                          value:(id)paragraphStyle  
    18.                          range:NSMakeRange(0, attributedString.length)];  

    -----------------------------------------猥琐的分界线-----------------------------------------

    这并不是唯一的方法,还有另一种替代方案:

    1. CATextLayer *textLayer = [CATextLayer layer];  
    1. textLayer.string = getAttributedString();  
    2. textLayer.frame = CGRectMake(0, CGRectGetMaxY(view.frame), 200, 200);  
    3. [self.view.layer addSublayer:textLayer];  


    CATextLayer可以直接支持NSAttributedString!

    -----------------------------------------猥琐的分界线-----------------------------------------

    效果图:

    源码地址

    -----------------------------------------猥琐的分界线-----------------------------------------

    目前发现有一个问题,好像中文全都会被加粗,设置不加粗的字体也没用,应该是CoreText的bug,已经上报给了apple了。

    -----------------------------------------对于cythb兄提出的问题-----------------------------------------

    首先表示感谢关注

    原文中确有描述不适当的地方,比如:The upper-left corner of the context is (0, 0) 。实际上Quartz2D的坐标系统确实在左下角,只是有一些技术在设置它们的graphics context时使用了不同于Quartz的默认坐标系统。相对于Quartz来说,这些坐标系统是修改的坐标系统(modified coordinate system)。

     

    UPDATED


    在iOS6之后,创建一个AttributedString变成了一件轻松的事情,<CoreText/CoreText.h>已经不需要导入了。如果我要设置字体的颜色,可以直接这样:

    [textAttr addAttribute:NSForegroundColorAttributeName

                     value:[UIColor redColor]

                     range:NSMakeRange(0, text.length)];

    如果要计算一个NSAttributedString的size,使用NSAttributedString的这个API:

    - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(6_0);


    但是需要注意一点,如果调用这个API的NSAttributedString不包含字体、行高等有利于计算的数据,那最终计算出来的size可能和实际有所出入。


    源文档 <http://blog.csdn.net/zhangao0086/article/details/7616385

0 0
原创粉丝点击