Apple Documentation (2) NSAttributedString

富文本NSAttributedString,有属性的字符串,可以设置字体,字号,颜色等属性。

以UILable为例子写一段代码,展示下图效果


Example.png
    //创建一个字符串
    NSString* str = @"Wecome to \nNews";
    
    //根据字符串创建相对应富文本对象
    NSMutableAttributedString* attriString = [[NSMutableAttributedString alloc]initWithString:str];
    
    //对富文本对象添加属性
    [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 9)];
    
    [attriString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-Bold" size:25] range:NSMakeRange(0, 9)];
    
    [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(10, 5)];
    
    [attriString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-Bold" size:25] range:NSMakeRange(10, 5)];
    
    //讲对象赋值给lab
    lab.attributedText = attriString;

要注意的是NSMakeRange(0, 9) 这个方法,不是取值0—9 而是指从0开始到后面的9个字符,所以NSMakeRange(10, 5)指的就是从10开始之后的5个字符。
该文本分为两行,所以在NSString中用"\n",来代表回车换行。

这里也要注意一点,只有做了以下设置时,换行字符才会生效

lab.numberOfLines = 0

然后既然有分行分段,那就必然有段落,也就是NSMutableParagraphStyle(段落样式)
以下是NSMutableParagraphStyle的一些常用属性

    @property(readwrite) CGFloat lineSpacing;                       //行间距
    @property(readwrite) CGFloat paragraphSpacing;                  //段间距
    @property(readwrite) NSTextAlignment alignment;                 //对齐方式
    @property(readwrite) CGFloat firstLineHeadIndent;               //首行缩紧
    @property(readwrite) CGFloat headIndent;                        //除首行之外其他行缩进
    @property(readwrite) CGFloat tailIndent;                        //每行容纳字符的宽度
    @property(readwrite) NSLineBreakMode lineBreakMode;             //换行方式
    @property(readwrite) CGFloat minimumLineHeight;                 //最小行高
    @property(readwrite) CGFloat maximumLineHeight;                 //最大行高
     //书写方式(NSWritingDirectionNatural,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)
    @property(readwrite) NSWritingDirection baseWritingDirection;
    @property(readwrite) CGFloat lineHeightMultiple;
    @property(readwrite) CGFloat paragraphSpacingBefore;
    @property(readwrite) float hyphenationFactor;
    @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
    @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);

照例上代码

NSString* str = @"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. An association of characters and their attributes is called an attributed string. The cluster’s two public classes, NSAttributedString and NSMutableAttributedString, declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively.";

不做任何处理的情况

Apple Documentation <Foundation> (2) NSAttributedString_第1张图片
不作处理图.png

然后我们使用上面的东西对文本进行下优化

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    paragraphStyle.firstLineHeadIndent = 20.0;
    paragraphStyle.lineSpacing = 10.0;
    paragraphStyle.paragraphSpacing = 20.0;
    [attriString addAttribute:NSParagraphStyleAttributeName
                                   value:paragraphStyle
                                   range:NSMakeRange(0, str.length)];
Apple Documentation <Foundation> (2) NSAttributedString_第2张图片
处理后.png

相对而言是不是稍微好看点?(才怪~)

嗯,除了这些之外还有好多属性,用到再去实现
ヾ( ̄▽ ̄)

    NSForegroundColorAttributeName//文字前景色
    
    NSBackgroundColorAttributeName//文字背景色
    
    NSLigatureAttributeName//连体字(NSNumber  @0:无连体,@1:默认连体,系统字体不包含对连体的支持)
    
    NSUnderlineStyleAttributeName//下划线
    
    NSStrokeColorAttributeName//只有在NSStrokeWidthAttributeName设置了值之后才有效(默认字体颜色和前景色一致,如果设置的颜色和前景色不一致则前景色无效)
    
    NSStrokeWidthAttributeName            //设置该属性之后字体变成空心字体,字体边线宽度为value设定的值
    
    NSBaselineOffsetAttributeName//值为NSNumber类型,表明文字相对于其他文字基准线向上的偏移量
    
    NSUnderlineColorAttributeName//值为UIColor类型,下划线颜色(只有在NSUnderlineStyleAttributeName的value为@1时有效)
    
    NSUnderlineStyleAttributeName//值为NSNumber类型,下划线宽度(默认值为@0:下划线宽度为0——不现实下划线,@1:字符串有下划线)

明天~NSAutoreleasePool——自动释放池( ⊙ o ⊙ )啊!

你可能感兴趣的:(Apple Documentation (2) NSAttributedString)