NSAttributedString 帶屬性的字符串
- set都是改變之前的設置或取代同類.比如設置代理,通常代理只是一個對象屬性,重復設置,也只有一個,具體起作用看情況
- add都是添加,補充之前的設置,或添加同類.比如添加target,添加消息,addOberver等等,都是添加到集合,每個添加的都有效

// setAttributes:(字典),是設置改變Attributes字典指向(Attributes字典存字符串屬性的所有鍵值對),改變屬性字典指向了,之前設置的無效
//addAttribute:(鍵值對)/addAttributes:(字典)往Attributes字典調(diào)加屬性設置的鍵值對或字典,未改變屬性字典指向,之前設置的非同一key的有效.
//方法名規(guī)律
// set都是改變之前的設置或取代同類.add都是添加,補充之前的設置,或添加同類
NSString:NSObject//無屬性字符串,不可變
NSAtrributedString:NSObject//帶屬性字符串
//屬性和字符串都不可變,一創(chuàng)建好就不可變
每個字符獨立接收屬性(range,批量設置范圍內(nèi)的所有字符,但每個字符獨立接收屬性)
重復設置同一字符(在同一字符串中位置相同),最后一次有效
- 例子利用TextField的attributedPlaceholder來探討NSAttributedString
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder;
- NSAttributedString不可變.(類NSString)字符串和屬性在創(chuàng)建后,不可變.(用之創(chuàng)建新字符串不是)
帶有屬性的字符串, 富文本
由2部分組成
文字內(nèi)容 : NSString *
文字屬性 : NSDictionary *
文字顏色 - NSForegroundColorAttributeName
字體大小 - NSFontAttributeName
下劃線 - NSUnderlineStyleAttributeName
背景色 - NSBackgroundColorAttributeName
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
attributes[NSBackgroundColorAttributeName] = [UIColor redColor];
attributes[NSUnderlineStyleAttributeName] = @YES;
//創(chuàng)建新的屬性字符串,創(chuàng)建后不可變(非指針指向不可變,而是字符串本身和屬性不可變).所以創(chuàng)建時就要設置好屬性
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attributes];
- NSMutableAttributedString可變(類NSMutableString)字符串和屬性在創(chuàng)建后,可變.
////創(chuàng)建新的屬性字符串,創(chuàng)建后可變,
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
// 創(chuàng)建后改變屬性
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
//1set
[string setAttributes:attributes range:NSMakeRange(0, 1)];
NSMutableDictionary *attributes2 = [NSMutableDictionary dictionary];
attributes2[NSBackgroundColorAttributeName] = [UIColor redColor];
attributes2[NSUnderlineStyleAttributeName] = @YES;
// setAttributes:(字典),是設置改變Attributes字典指向(Attributes字典存字符串屬性的所有鍵值對),改變屬性字典指向了,之前設置的//1set無效,因為屬性作用是在每個字符的,//2setRange(0, 2)包含了//1setRange(0, 1)中的字符所以//1set被覆蓋了屬性
//2set
[string setAttributes:attributes2 range:NSMakeRange(0, 2)];
//addAttribute:(鍵值對)/addAttributes:(字典)往Attributes字典調(diào)加屬性設置的鍵值對或字典,未改變屬性字典指向,之前設置的非同一key的有效.
[string addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 1)];
[string addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
[string addAttribute:NSUnderlineStyleAttributeName value:@YES range:NSMakeRange(1, 1)];
self.attributedPlaceholder = string;
UILabel *label = [[UILabel alloc] init];
// label.text = @"你好哈哈哈";
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"你好哈哈哈"];
[text addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, 3)];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)];
label.attributedText = text;
label.frame = CGRectMake(100, 100, 100, 25);
[self.view addSubview:label];
UILabel *label = [[UILabel alloc] init];
// 設置屬性文字
NSString *text = @"你好\n哈哈哈";
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, text.length)];
[attributedText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 3)];
label.attributedText = attributedText;
// 其他設置
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
label.frame = CGRectMake(0, 0, 100, 40);
[self.view addSubview:label];
self.navigationItem.titleView = label;
- 利用屬性字符串,進行l(wèi)abel的圖文混排
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(100, 100, 200, 25);
label.backgroundColor = [UIColor redColor];
label.font = [UIFont systemFontOfSize:14];
[self.view addSubview:label];
// 圖文混排
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];
// 1 - 你好
NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"你好"];
[attributedText appendAttributedString:first];
// 2 - 圖片
// 帶有圖片的附件對象
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"header_cry_icon"];
CGFloat lineH = label.font.lineHeight;
attachment.bounds = CGRectMake(0, - ((label.xmg_height - lineH) * 0.5 - 1), lineH, lineH);
// 將附件對象包裝成一個屬性文字
NSAttributedString *second = [NSAttributedString attributedStringWithAttachment:attachment];
[attributedText appendAttributedString:second];
// 3 - 哈哈哈
NSAttributedString *third = [[NSAttributedString alloc] initWithString:@"哈哈哈"];
[attributedText appendAttributedString:third];
label.attributedText = attributedText;