NSAttributedString 带属性的字符串
- set都是改变之前的设置或取代同类.比如设置代理,通常代理只是一个对象属性,重复设置,也只有一个,具体起作用看情况
- add都是添加,补充之前的设置,或添加同类.比如添加target,添加消息,addOberver等等,都是添加到集合,每个添加的都有效
![image](images/屏幕快照 2015-11-09 15.25.16.png)
// setAttributes:(字典),是设置改变Attributes字典指向(Attributes字典存字符串属性的所有键值对),改变属性字典指向了,之前设置的无效
//addAttribute:(键值对)/addAttributes:(字典)往Attributes字典调加属性设置的键值对或字典,未改变属性字典指向,之前设置的非同一key的有效.
//方法名规律
// set都是改变之前的设置或取代同类.add都是添加,补充之前的设置,或添加同类
NSString:NSObject//无属性字符串,不可变
NSAtrributedString:NSObject//带属性字符串
//属性和字符串都不可变,一创建好就不可变
每个字符独立接收属性(range,批量设置范围内的所有字符,但每个字符独立接收属性)
重复设置同一字符(在同一字符串中位置相同),最后一次有效
- 例子利用TextField的attributedPlaceholder来探讨NSAttributedString
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder;
- NSAttributedString不可变.(类NSString)字符串和属性在创建后,不可变.(用之创建新字符串不是)
带有属性的字符串, 富文本
由2部分组成
文字内容 : NSString *
文字属性 : NSDictionary *
文字颜色 - NSForegroundColorAttributeName
字体大小 - NSFontAttributeName
下划线 - NSUnderlineStyleAttributeName
背景色 - NSBackgroundColorAttributeName
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor yellowColor];
attributes[NSBackgroundColorAttributeName] = [UIColor redColor];
attributes[NSUnderlineStyleAttributeName] = @YES;
//创建新的属性字符串,创建后不可变(非指针指向不可变,而是字符串本身和属性不可变).所以创建时就要设置好属性
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attributes];
- NSMutableAttributedString可变(类NSMutableString)字符串和属性在创建后,可变.
////创建新的属性字符串,创建后可变,
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.placeholder];
// 创建后改变属性
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字典调加属性设置的键值对或字典,未改变属性字典指向,之前设置的非同一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;
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;
网友评论