Apple Documentation <Foundation> (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.";

不做任何处理的情况

不作处理图.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)];
处理后.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 ⊙ )啊!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 标签: 技术分享 以前看到这种字号和颜色不一样的字符串,想出个讨巧的办法就是“¥150”一个UILable,“元/...
    zhong_JF阅读 1,680评论 0 0
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,272评论 0 4
  • { 24、Sqlite数据库 1、存储大数据量,增删改查,常见管理系统:Oracle、MSSQLServer、DB...
    CYC666阅读 964评论 0 1
  • 今天去了趟婚博会,本来是奔着领免费礼品去的,结果发现我们没有领免费礼品的资格…… 无奈地转了一圈,还是发现了几件有...
    郁金苏合香阅读 447评论 0 3
  • 我记得我懂事以后第一次看见女人抽烟 不觉得很酷 相反 我觉得女人怎么可以抽烟呢 柔曼温暖的女人怎么能抽烟 后来我也...
    13821b53e168阅读 414评论 0 0