//
//  NSAttributedString+GM.m
//  Gengmei
//
//  Created by MoMo on 2019/12/27.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "NSAttributedString+GM.h"
#import "NSString+GM.h"
#import "NSAttributedString+GMSize.h"

#define GMMoneyRMB  @"¥" // 人民币符号

@implementation NSAttributedString (GM)

/// 获取富文本后的价格展示
/// @param priceName 价格名称“促销价、预付款”
/// @param nomalColor 通用颜色
/// @param nomalFont 通用字体
/// @param integer 整数部分
/// @param integerColor 整数颜色 (默认通用)
/// @param integerFont 整数字体 (默认通用)
/// @param decimal 小数
/// @param decimalColor 小数颜色 (默认通用)
/// @param decimalFont 小数字体 (默认通用)
+ (NSMutableAttributedString *)welfarePriceName:(NSString *)priceName nomalColor:(UIColor *)nomalColor nomalFont:(UIFont *)nomalFont integer:(NSString *)integer integerColor:(UIColor *)integerColor integerFont:(UIFont *)integerFont decimal:(NSString *)decimal decimalColor:(UIColor *)decimalColor decimalFont:(UIFont *)decimalFont {
    
    integerColor = integerColor?:nomalColor;
    integerFont = integerFont?:nomalFont;
    decimalColor = decimalColor?:nomalColor;
    decimalFont = decimalFont?:nomalFont;
    
    NSMutableAttributedString *welfarePriceAttibute = [NSMutableAttributedString attributedStringWithString:priceName font:nomalFont color:nomalColor];
    if ([integer isNonEmpty]) {
        NSAttributedString *integerAttibute = [NSAttributedString attributedStringWithString:integer font:integerFont color:integerColor];
        [welfarePriceAttibute appendAttributedString:integerAttibute];
    }
    if ([decimal isNonEmpty]) {
        NSAttributedString *decimalAttibute = [NSAttributedString attributedStringWithString:integer font:decimalFont color:decimalColor];
        [welfarePriceAttibute appendAttributedString:decimalAttibute];
    }
    // 找到人民币符号，将其字体设置为nomalFont 颜色设置为integerColor
    NSRange rmbRange = [welfarePriceAttibute.string rangeOfString:GMMoneyRMB];
    if (rmbRange.location != NSNotFound) {
        [welfarePriceAttibute addAttributes:@{NSFontAttributeName:nomalFont, NSForegroundColorAttributeName:integerColor} range:rmbRange];
    }
    
    return welfarePriceAttibute;
}

@end
