今天突然有这么一个需求:
3行的UILabel,每行字体的颜色、大小不同。
解决办法:利用字符串分割和富文本编辑
Talk is too cheap,Show you my code。
NSString*str =@"中华人民\n隆起南阳\n南沙群岛";
NSArray*strArr = [str componentsSeparatedByString:@"\n"];
NSMutableAttributedString*attributeStr = [[NSMutableAttributedString alloc]initWithString:str];
[attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25]range:NSMakeRange(0,4)];
[attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13]range:NSMakeRange(5,4)];
[attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor]range:NSMakeRange(10,4)];
UILabel*label = [[UILabel alloc]initWithFrame:CGRectMake(20,200,380,300)];
label.backgroundColor= [UIColor lightGrayColor];
label.numberOfLines=3;
label.attributedText= attributeStr;
[self.view addSubview:label];
效果: