label中存在attributedText
屬性
- 設(shè)置整體的富文本
UILabel *label = [[UILabel alloc]init];
NSString *string = @"這是一段話,用于實(shí)現(xiàn)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.分開設(shè)置
UILabel *label = [[UILabel alloc]init];
NSString *string = @"這是一段話棕所,用于實(shí)現(xiàn)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];