美文网首页
改变 string(label)上 不一样的文字 颜色 字体

改变 string(label)上 不一样的文字 颜色 字体

作者: 奕珃 | 来源:发表于2017-11-17 10:50 被阅读0次

label-------------------------------------------------------

- (void)totalLabelText:(UILabel*)label {

label.text=@"共计3件商品合计:¥333.00 (含运费¥5.00)";

// 创建Attributed

NSMutableAttributedString*noteStr = [[NSMutableAttributedString alloc]initWithString:label.text];

// 需要改变的第一个文字的位置

NSUInteger firstLoc = [[noteStrstring]rangeOfString:@"("].location;

// 需要改变的最后一个文字的位置

NSUInteger secondLoc = [[noteStrstring]rangeOfString:@")"].location+1;

// 需要改变的区间

NSRange range =NSMakeRange(firstLoc, secondLoc - firstLoc);

// 改变颜色

//[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:range];

// 改变字体大小及类型

//[noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-BoldOblique" size:27] range:range];

[noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12]range:range];

// 为label添加Attributed

[label setAttributedText:noteStr];

}

string---------------------------------------------------------

NSString* str = [NSStringstringWithFormat:@"共计%ld件商品合计:¥%@",(long)commGoodsOrderModel.details.count, [PromptBoxViewchangeformatterWithFen:commGoodsOrderModel.gooods_amount]];

NSString* freightStr = [NSStringstringWithFormat:@" (含运费¥%@)", [PromptBoxViewchangeformatterWithFen:commGoodsOrderModel.freight]];

// 创建Attributed

NSMutableAttributedString*noteStr = [[NSMutableAttributedStringalloc]initWithString:[NSStringstringWithFormat:@"%@%@",str, freightStr]];

NSRangerange =NSMakeRange([[noteStrstring]rangeOfString:freightStr].location, [[noteStrstring]rangeOfString:freightStr].length);

[noteStraddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:12]range:range];

[self.totalLabel setAttributedText:noteStr];

相关文章

网友评论

      本文标题:改变 string(label)上 不一样的文字 颜色 字体

      本文链接:https://www.haomeiwen.com/subject/umidvxtx.html