今天突然有這么一個(gè)需求:
? ? ? ? ? ? ? ?3行的UILabel,每行字體的顏色息楔、大小不同寝贡。
解決辦法:利用字符串分割和富文本編輯
Talk is too cheap,Show you my code值依。
NSString*str =@"中華人民\n隆起南陽(yáng)\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];
效果: