NSMutableAttributedString的详细使用(一)

#向参考的博客原文作者致敬
#参考链接:
http://www.jianshu.com/p/8f49c9c99b21
http://www.cnblogs.com/wangbinios/p/6123946.html
http://blog.csdn.net/liujinlongxa/article/details/44840753
#待学习链接
http://www.itnose.net/detail/6177538.html
http://blog.csdn.net/ys410900345/article/details/25976179
http://www.cocoachina.com/ios/20160512/16223.html

本篇博客主要是NSMutableAttributedString的简介篇,详细篇后续有时间跟新,欢迎大家批评改正。

一、创建方式

简述:

NSMutableAttributedString适用的控件非常多,例如:UITextViewUILabelUIButton....等,这里总结就以《UILabel》为例子。
《1》、方式一:
/* 创建一个label */
 UILabel *testLabel = [[UILabel  alloc] initWithFrame:CGRectMake(30, 60, 200, 200)];
/* 自动换行 */
 testLabel.numberOfLines = 0;
/* 初始化的字符串 */
 NSString  *testStr = @"年轻人就要多去挑战,要让不可能成为可能";
#/* 初始化的方式创建 */
 NSMutableAttributedString *attributedString = [[NSMutableAttributedString  alloc] initWithString:testStr];
#//也可以使用此方法初始化创建- (instancetype)initWithString:(NSString *)str;
/****** 最基本的添加属性的方式,使用哪一个属性就添加哪一个 ,这里只是随便使用了几个属性后续有时间还会一一介绍。******/
/*****
    *@Attribute  参数含义:添加的属性名称
    *@value:属性所需要赋的值
    *@range:  添加的属性适应的范围
    ************/
 [attributedString  addAttribute:NSFontAttributeName value:[UIFont fontWithName: @"Zapfino" size: 15] range:NSMakeRange(0, testStr.length)];
 [attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 4)];
 [attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(4, 3)];
#/* 这里需要注意UILabel之前是使用Label的`text`属性进行赋值的,但是使用NSMutableAttributedString设置完文本后就要改成控件的`attributedText`属性进行赋值否则会有警告并且赋值不成功。*/
 testLabel.attributedText = attributedString;
 [self.view addSubview:testLabel]
《2》、方式二:
/* 创建一个label */
 UILabel *testLabel = [[UILabel  alloc] initWithFrame:CGRectMake(30, 60, 200, 200)];
/* 自动换行 */
 testLabel.numberOfLines = 0;
/* 初始化的字符串 */
 NSString  *testStr = @"年轻人就要多去挑战,要让不可能成为可能";
#/*使用字典创建,然后直接将要设置的属性添加到字典中  */
NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15], NSForegroundColorAttributeName: [UIColor blueColor] };
#/* 这里使用NSMutableAttributedString中自带的一个个实例方法将刚才设置的全部属性添加进去*/
testLabel.attributedText = [[NSAttributedString alloc] initWithString: testStr attributes: attrDict];
 [self.view  addSubview:testLabel];

二、方法简介

《1》、常用方法简介
/* 初始化方法 */
- (instancetype)initWithString:(NSString *)str;
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *, id> *)attrs;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
/* 为某一范围内文字设置多个属性 */
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
/* 为某一范围内文字添加某个属性 */
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
/* 为某一范围内文字添加多个属性 */
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
/*移除某范围内的某个属性*/
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
《2》、详细介绍一些方法
[1]、获取指定位置上的属性信息,并返回与指定位置属性相同并且连续的字符串的范围信息。
- (NSDictionary<NSString *, id> *)attributesAtIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range;
#例如:
  NSRange range = NSMakeRange(0, 2);
  NSDictionary*dic = [attributedString attributesAtIndex:2 effectiveRange:&range];
#参数介绍:    
@location :为位置信息  对应例子中attributesAtIndex:2   
@effectiveRange  有效范围  对应例子中&range
[2]、获取指定范围内的子字符串的信息
#参数介绍:
@NSAttributedString   返回一个NSAttributedString是咧对象
@range  从哪一个范围获取
- (NSAttributedString *)attributedSubstringFromRange:(NSRange)range;
[3]、** 在Range中查找string从attributesAtIndex:开始找,找到的第一个属性,即attributeDict,把属性的位置传给longestEffectiveRange:属性 **
#参数介绍:
@inRange   在字符串中的范围查找
@attributesAtIndex  从哪一个索引开始查找,找到第一个属性
@longestEffectiveRange    将第一个属性的位置传过来
#/* 方法 */
- (NSDictionary<NSString *, id> *)attributesAtIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;
能成为可能";
#范例:
/* 创建一个label */ UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 60, 200, 200)];
 [self.view  addSubview:testLabel];
 NSString  *testStr = @"年轻人就要多去挑战,要让不可能成为可能";
 NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15], NSForegroundColorAttributeName: [UIColor blueColor] };
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:testStr attributes:attrDict];

 [attributedString  addAttribute:NSForegroundColorAttributeNamevalue: [UIColor redColor]    range:NSMakeRange(7, 5)];
NSDictionary *attributeDict;
NSRange effectiveRange = { 0, 10 };
do {        
 NSRange  range = NSMakeRange (NSMaxRange(effectiveRange), [attributedString length] - NSMaxRange(effectiveRange));
 attributeDict = [attributedString attributesAtIndex: range.location   longestEffectiveRange: &effectiveRange  inRange: range];
 NSLog (@"Range: %@  Attributes: %@",
NSStringFromRange(effectiveRange), attributeDict);
 testLabel.attributedText = attributedString;
} while (NSMaxRange(effectiveRange) < [attributedString length]);
      [4]、判断两个NSAttributedString是否相等
- (BOOL)isEqualToAttributedString:(NSAttributedString *)other;

三、属性简介

1、NSFontAttributeName(字体)

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

2、NSParagraphStyleAttributeName(段落)

** 该属性所对应的值是一个 NSParagraphStyle 对象。该属性在一段文本上应用多个属性。如果不指定该属性,则默认为 NSParagraphStyle 的defaultParagraphStyle 方法返回的默认段落属性。**

3、NSForegroundColorAttributeName(字体颜色)

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

4、NSBackgroundColorAttributeName(字体背景色)

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

5、NSLigatureAttributeName(连字符)

   该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。
- 0 表示没有连体字符。
- 1 表示使用默认的连体字符。
- 2表示使用所有连体符号。
- 默认值为 1(注意,iOS 不支持值为 2)

6、NSKernAttributeName(字间距)

** NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄**

7、NSStrikethroughStyleAttributeName(删除线)

NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值:
NSUnderlineStyleNone。/*  默认值 */
NSUnderlineStyleNone /* 不设置删除线 */
NSUnderlineStyleSingle/*  设置删除线为细单实线 */
NSUnderlineStyleThick/*  设置删除线为粗单实线 */
NSUnderlineStyleDouble /* 设置删除线为细双实线 */

8、NSStrikethroughColorAttributeName (删除线颜色)

** NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色**

9、NSUnderlineStyleAttributeName(下划线)

该属性所对应的值是一个 NSNumber 对象(整数)。
该值指定是否在文字上加上下划线,该值参考“Underline Style Attributes”。
默认值是NSUnderlineStyleNone。
#下划线除了线条位置和删除线不同外,其他的都可以完全参照删除线设置。

10、NSUnderlineColorAttributeName(下划线颜色)

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

11、NSStrokeColorAttributeName(边线颜色)

12、NSStrokeWidthAttributeName(边线宽度)

13、NSShadowAttributeName(阴影)

该属性所对应的值是一个 NSShadow 对象。默认为 nil。单独设置不好使,和这三个任一个都好使,NSVerticalGlyphFormAttributeName,
NSObliquenessAttributeName,
NSExpansionAttributeName

14、NSVerticalGlyphFormAttributeName(横竖排版)

** 该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。**

15、NSObliquenessAttributeName(字体倾斜)

16、NSExpansionAttributeName (文本扁平化)

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

推荐阅读更多精彩内容