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

2017-11-17  本文已影响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];

上一篇 下一篇

猜你喜欢

热点阅读