iOS开发实现Label中多颜色多字体

时间:2023-03-09 09:39:18
iOS开发实现Label中多颜色多字体

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(8, 100, 300, 30)];

label.textColor = wordGaryColor;  //未专门设置的字体颜色

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"公司简介:发挥收到后即可圣诞节快发吧士大夫深刻的房价高速度开放和山东矿机山东科技楼粉红是大家分工"];

[str addAttribute:NSForegroundColorAttributeName value:commonColor range:NSMakeRange(0,5)];

   [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
   [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
   [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:19] range:NSMakeRange(0, 5)];
   [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
   [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];

label.attributedText = str;

[self.view addSubview:label];