iOS开发:字符串设置指定内容的文字颜色、文字大小、文字字体类型
一、字符串设置部分文字的字体颜色和字体大小
NSString *Str=@"当前995人通过星团优惠预定酒店";
NSRange hightlightTextRange = [@"当前995人通过星团优惠预定酒店" rangeOfString:@"995人"];
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:Str];
[attributeStr addAttribute:NSForegroundColorAttributeName
value:[UIColor colorWithHexString:@"#1EC46D"]
range:hightlightTextRange];
[attributeStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13.0f] range:hightlightTextRange];
self.peopleNumLa.lineBreakMode = NSLineBreakByTruncatingMiddle;
self.peopleNumLa.attributedText = attributeStr;
二、字符串中部分文字划删除线
NSString * price = @"¥65.00";
NSString *market_price = @"原价¥756.00";
NSMutableAttributedString * ma_price = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@",price,market_price]];
[ma_price addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15 weight:0.1] range:NSMakeRange(0, price.length)];
[ma_price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, price.length)];
[ma_price addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange( price.length+1, market_price.length)];
[ma_price addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange( price.length+1, market_price.length)];
[ma_price addAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)} range:NSMakeRange( price.length+1, market_price.length)];
_priceLa.attributedText = ma_price;
网友评论