价格标签的富文本处理:
- (NSMutableAttributedString *)generateAmountWithPrice:(NSString *)price unitSize:(UIFont *)unitFont priceSize:(UIFont *)priceFont dotFont:(UIFont *)dotFont {
if (price.length) {
NSMutableAttributedString *attributedPrice = [[NSMutableAttributedString alloc] initWithString:price];
[attributedPrice addAttribute:NSFontAttributeName value:priceFont range:[price rangeOfString:price]];
//判断是否有羊角,羊角字体变小
if ([price rangeOfString:@"¥"].location != NSNotFound) {
[attributedPrice addAttribute:NSFontAttributeName value:unitFont range:[price rangeOfString:@"¥"]];
}
//判断是否有折,折字体变小
if ([price rangeOfString:@"折"].location != NSNotFound) {
[attributedPrice addAttribute:NSFontAttributeName value:unitFont range:[price rangeOfString:@"折"]];
//判断是否有小数点,小数点后字体变小
NSRange dotRange = [price rangeOfString:@"."];
if (dotRange.location != NSNotFound) {
NSString *string = [price substringWithRange:NSMakeRange(dotRange.location, [price rangeOfString:@"折"].location-dotRange.location)];
if (string.length > 0) {
[attributedPrice addAttribute:NSFontAttributeName value:dotFont range:[price rangeOfString:string]];
}
}
} else {
//判断是否有小数点,小数点后字体变小
if ([price rangeOfString:@"."].location != NSNotFound) {
NSRange rang = [price rangeOfString:@"."];
NSString *string = [price substringFromIndex:rang.location];
if (string.length > 0) {
[attributedPrice addAttribute:NSFontAttributeName value:dotFont range:[price rangeOfString:string]];
}
}
}
return attributedPrice;
} else {
return [[NSMutableAttributedString alloc] initWithString:@""];
}
}
网友评论