- 今天接觸了屬性字符串的部分用法痢站,發(fā)現(xiàn)真是好用把邕帧!以下是部分用法:
NSMutableAttributedString* attributedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"合計(jì): ¥%.2f",model.goodsCount * model.goodsModel.price]];
NSRange range1 = NSMakeRange(0, 3);
NSRange range2 = NSMakeRange(3, attributedStr.length - 3);
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#6c6c6c"] range:range1];//設(shè)置前面三個(gè)字符的顏色
[attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.f] range:range1];//設(shè)置前面三個(gè)字符的字號(hào)大小
[attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18.f] range:range2];//設(shè)置后面字符的顏色
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#fc4001"] range:range2];//設(shè)置后面字符的字號(hào)大小
UILable* label=[ [UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 64)]];
label.attributedText = attributedStr;
其實(shí)際效果如下:
屬性字符串顯示效果.png