适用于:当你想对一个字符串中的某几个字符更改颜色,字体。。。
NSString *string = @"今日营养配餐提供热量1800千卡,需要饮食之外额外补充钙10mg,铁20mg,锌9.5mg,叶酸200μgDFE,维生素D 10ug,维生素B1 1.2mg,维生素B2 1.2mg。";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range = [string rangeOfString:@"今日营养配餐"];
[attributedString addAttribute:NSForegroundColorAttributeName value:JYEColor(72, 75, 91) range:range];
[attributedString addAttribute:NSFontAttributeName value:[self jyeFontWithName:@"PingFangSC-Medium" size:13] range:range];
hotLabel.attributedText = attributedString;
使用场景:处理 属性字符串 文字居中
NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc]init];
paragrapStyle.alignment = NSTextAlignmentCenter;//文字居中属性
NSString * shareCountStr = [NSString stringWithFormat:@"———已有%@人分享———",self.dishAdviceShareCountString];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:shareCountStr];
NSRange range = [shareCountStr rangeOfString:self.dishAdviceShareCountString];//人数 做颜色处理
[attributedString addAttribute:NSForegroundColorAttributeName value:JYEColor(255, 48, 99) range:range];
[attributedString addAttribute:NSFontAttributeName value:[JYEToolModel jyeFontWithName:@"PingFangSC-Regular" size:12] range:range];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:range];
shareNumLabel.attributedText = attributedString;