label中存在attributedText
属性
- 设置整体的富文本
UILabel *label = [[UILabel alloc]init];
NSString *string = @"这是一段话,用于实现label的富文本";
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:string];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont fontWithName:@"Zapfino" size:10];
dict[NSForegroundColorAttributeName] = [UIColor redColor];
NSRange range = NSMakeRange(2, 2);
[attribute setAttributes:dict range:range];
label.attributedText = attribute;
label.numberOfLines = 0;
label.frame = CGRectMake(100, 100, 200, 100);
[self.view addSubview:label];
2.分开设置
UILabel *label = [[UILabel alloc]init];
NSString *string = @"这是一段话,用于实现label的富文本";
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:string];
NSRange range = NSMakeRange(2, 2);
[attribute addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Zapfino" size:10] range:range];
[attribute addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
label.attributedText = attribute;
label.numberOfLines = 0;
label.frame = CGRectMake(100, 100, 200, 100);
[self.view addSubview:label];