美文网首页
一段价格标签的富文本

一段价格标签的富文本

作者: 大成小栈 | 来源:发表于2022-07-21 21:41 被阅读0次

价格标签的富文本处理:

- (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:@""];
    }
}

相关文章

网友评论

      本文标题:一段价格标签的富文本

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