iOS 富文本属性NSMutableAttributedString使用详解

前言:

        在iOS中,显示一段字符串通常会使用UILabel,但是它对文字的展现方式比较单一,有的时候需要展示一些特殊的形式,此时NSMutableAttributedString可以很好地满足使用要求。它一共提供了21方法可以选择使用,下面我将一一介绍。

一:属性介绍

1:NSParagraphStyleAttributeName 段落排版

       设置文本段落排版格式,取值为 NSParagraphStyle 对象,该属性在一段文本上应用多个属性。如果不指定该属性,则默认为 NSParagraphStyle 的defaultParagraphStyle 方法返回的默认段落属性。

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

style.firstLineHeadIndent = 20; 

style.lineSpacing = 20;

style.alignment = NSTextAlignmentCenter;

2:NSFontAttributeName 字体属性

      该属性所对应的值是一个 UIFont 对象。该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)。

3:NSForegroundColorAttributeNam  字体颜色

       该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。

       NSForegroundColorAttributeName 设置的颜色与 UILabel 的 textColor 属性设置的颜色在地位上是相等的,与 NSBackgroundColorAttributeName 地位上也相等,谁最后赋值,最终显示的就是谁的颜色,但是textColor属性可以与 NSBackgroundColorAttributeName 属性可叠加。

4:NSBackgroundColorAttributeName  字体所在区域背景颜色

      该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的背景颜色。如果不指定该属性, 默认值为nil, 透明色

5:NSLigatureAttributeName  连体属性

        该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1 表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)。一般中文用不到,在英文中可能出现相邻字母连笔的情况

6:NSKernAttributeName  字符间距

取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄,值为浮点数,字距属性,默认值为0

7:NSStrikethroughStyleAttributeName  删除线

设置删除线,取值为 NSNumber 对象(整数),枚举默认值是NSUnderlineStyleNone。

NSUnderlineStyleNone 不设置删除线

NSUnderlineStyleSingle 设置删除线为细单实线

NSUnderlineStyleThick 设置删除线为粗单实线

NSUnderlineStyleDouble 设置删除线为细双实线

8:NSStrikethroughColorAttributeName  删除线颜色

取值为 UIColor 对象,默认值为黑色,

9:NSUnderlineStyleAttributeName 下划线

取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似,该值指定是否在文字上加上下划线,下划线除了线条位置和删除线不同外,其他的都可以完全参照删除线设置

10:NSUnderlineColorAttributeName 下划线颜色

设置下划线颜色,取值为 UIColor 对象,默认值为黑色

11:NSStrokeWidthAttributeName  笔画宽度

      值为浮点数NSNumber,该值改变笔画宽度(相对于字体 size 的百分比)设置笔画的粗细,负值填充效果,正值中空效果,正数只改变描边宽度,负数同时改变文字的描边和填充宽度,默认为 0,即不改变。

12:NSStrokeColorAttributeName 填充部分颜色

填充部分颜色,不是字体颜色,取值为 UIColor 对象,

13:NSShadowAttributeName  阴影属性

取值为 NSShadow 对象,默认为 nil。

NSShadow *shadow=[[NSShadow alloc] init];

shadow.shadowBlurRadius=5;//模糊度

shadow.shadowColor=[UIColor yellowColor];

shadow.shadowOffset=CGSizeMake(1, 3);

14:NSObliquenessAttributeName  字形倾斜度

取值为 NSNumber (float),正值右倾,负值左倾,默认值为0,表示没有倾斜,

15:NSExpansionAttributeName  横向拉伸属性,

取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本

16:NSVerticalGlyphFormAttributeName 文字排版方向

取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本,在iOS中, 总是以横向排版,0 以外的值都未定义。

17:NSTextEffectAttributeName 文本特殊效果

这个属性的值是一个NSString对象。使用此属性指定的文字效果,如NSTextEffectLetterpressStyle。此属性的默认值为nil,表示没有文本效应。

18:NSBaselineOffsetAttributeName  基线偏移值

取值为 NSNumber (float),表示的字符从基线偏移的NSNumber对象,默认值是0,正值上偏,负值下偏。

19:NSWritingDirectionAttributeName  文字书写方向

        取值为包含NSNumber对象的数组. 从左向右书写或者从右向左书写。

The values of the NSNumber objects should be 0, 1, 2, or 3, for LRE, RLE, LRO, or RLO respectively, and combinations of NSWritingDirectionLeftToRight and NSWritingDirectionRightToLeft with NSTextWritingDirectionEmbedding or NSTextWritingDirectionOverride, as shown in Values of NSWritingDirectionAttributeName and equivalent markup.

[attributedString addAttribute:NSWritingDirectionAttributeName value:@[@3]  range:NSMakeRange(40, 4)];

20:NSLinkAttributeName  链接属性

点击后调用浏览器打开指定URL地址,此属性的值是NSURL对象(首选)或一个NSString对象,此属性的默认值为nil,表示没有链接,UILabel无法使用该属性, 可以使用UITextView 控件。


21:NSAttachmentAttributeName  文本附件

      取值为NSTextAttachment对象,常用于文字图片混排,此属性的默认值为nil,表示无附件。

使用:

//创建NSTextAttachment的对象,用来装载图片

NSTextAttachment *attch = [[NSTextAttachment alloc] init];

//将NSTextAttachment对象的image属性设置为想要使用的图片

attch.image = [UIImage imageNamed:@"lock_wallpaper.jpeg"];

//设置NSTextAttachment对象bounds大小,也就是要显示的图片的大小

attch.bounds = CGRectMake(0, 0, 50, 50);

// 创建带有图片的富文本

NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];

//插入文本指定位置

[attributedString insertAttributedString:string atIndex:7];

//插入文本末尾

[attributedString appendAttributedString:string];


二:效果展示

      了解完上面的属性介绍,看一下实际的效果



三:实现代码:

- (void)viewDidLoad {

[super viewDidLoad];

self.lable.numberOfLines = 0;       //自动换行

//创建一个NSMutableAttributedString富文本对象

NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc] initWithString:@"愿你有好运气,如果没有,愿你在不幸中学会慈悲。愿你被很多人爱,如果没有,愿你在寂寞中学会宽容。愿你一生一世每天都可以睡到自然醒。"];

[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:40.0] range:NSMakeRange(0, 2)];    //设置字体属性 大小

[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 2)];     //设置字体颜色

[attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(4, 2)];   //设置字体所在区域背景颜色

[attributedString addAttribute: NSLigatureAttributeName value:@(1) range:NSMakeRange(7, 2)];    //设置连体属性

[attributedString addAttribute: NSKernAttributeName  value:@(10) range:NSMakeRange(7, 2)];  //设定字符间距

[attributedString addAttribute:NSStrikethroughStyleAttributeName  value:@(NSUnderlineStyleThick) range:NSMakeRange(9, 2)];  //设置删除线

[attributedString addAttribute: NSStrikethroughColorAttributeName  value:[UIColor redColor] range:NSMakeRange(9, 2)];  //设置删除线颜色

[attributedString addAttribute: NSUnderlineStyleAttributeName  value:@(NSUnderlineStyleThick) range:NSMakeRange(12, 2)];  //设置下划线

[attributedString addAttribute: NSUnderlineColorAttributeName  value:[UIColor redColor] range:NSMakeRange(12, 2)];  //设置下划线颜色

[attributedString addAttribute:NSStrokeWidthAttributeName value:@(-5) range:NSMakeRange(14, 2)];  //设置笔画宽度  //填充字

[attributedString addAttribute:NSStrokeWidthAttributeName value:@(2) range:NSMakeRange(16, 2)];//设置笔画宽度  //空心字

[attributedString addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(14, 4)];  //填充部分颜色

//设置阴影属性

NSShadow *shadow=[[NSShadow alloc] init];

shadow.shadowBlurRadius=5;//模糊度

shadow.shadowColor=[UIColor yellowColor];

shadow.shadowOffset=CGSizeMake(1, 3);

[attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(18, 4)];

[attributedString addAttribute:NSObliquenessAttributeName value:@(1) range:NSMakeRange(23, 2)];    //设置字形倾斜度,

[attributedString addAttribute:NSExpansionAttributeName value:@(0.5) range:NSMakeRange(25, 4)];    //设置文本横向拉伸属性,

[attributedString addAttribute:NSVerticalGlyphFormAttributeName value:@(1) range:NSMakeRange(18, 4)];  //设置文字排版方向,

[attributedString addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(31, 4)];  //设置文本特殊效果,目前只有图版印刷效果可用:

[attributedString addAttribute:NSBaselineOffsetAttributeName value:@(-6) range:NSMakeRange(36, 4)]; //设置基线偏移值,

[attributedString addAttribute:NSWritingDirectionAttributeName value:@[@3]  range:NSMakeRange(40, 4)];  //设置文字书写方向,

//设置链接属性, UILabel无法使用该属性, 可以使用UITextView 控件.

NSURL *url=[NSURL URLWithString:@"www.baidu.com"];

[attributedString addAttribute: NSLinkAttributeName  value:url  range:NSMakeRange(44, 2)];

//NSAttachmentAttributeName 设置文本附件,常用于文字图片混排

NSTextAttachment *attch = [[NSTextAttachment alloc] init];  //创建NSTextAttachment的对象,用来装载图片

attch.image = [UIImage imageNamed:@"lock_wallpaper.jpeg"];  //将NSTextAttachment对象的image属性设置为想要使用的图片

attch.bounds = CGRectMake(0, 0, 50, 50);  //设置NSTextAttachment对象bounds大小,也就是要显示的图片的大小

NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];  // 创建带有图片的富文本

[attributedString insertAttributedString:string atIndex:7];   //图片插入指定位置

[attributedString appendAttributedString:string];    //插入末尾

//设置文本段落排版格式,

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

style.firstLineHeadIndent = 20;

style.lineSpacing = 20;

style.alignment = NSTextAlignmentCenter;

[attributedString addAttribute:NSParagraphStyleAttributeName value:style  range:NSMakeRange(0, attributedString.length)];

self.lable.attributedText=attributedString;//设置控件的富文本对象

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容