根据需求使用第一个或第二个方法
/**
* 多样性字符串处理
*
* @param original 原始字符串
* @param conversion 需转换的字符串
* @param font 字体
* @param color 颜色
*
* @return 转换好的字符串
*/
- (NSMutableAttributedString *)diverseStringOriginalStr:(NSString *)original conversionStr:(NSString *)conversion withFont:(UIFont *)font withColor:(UIColor *)color
{
original = [NSString nullToString:original]; //非空判断,可以删了,记得不要传空就行
conversion = [NSString nullToString:conversion]; //非空判断,可以删了,记得不要传空就行
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:original];
NSRange range = [original rangeOfString:conversion];
[str addAttribute:NSForegroundColorAttributeName value:color range:range];
[str addAttribute:NSFontAttributeName value:font range:range];
return str;
}
使用示例
self.contentLabel.attributedText = [self diverseStringOriginalStr:@"招商银行(尾号1802 储蓄卡)" conversionStr:@"(尾号1802 储蓄卡)" withFont:[UIFont systemFontOfSize:14] withColor:
[UIColor redColor]]
/**
* 多样性字符串处理(批量处理)
*
* @param original 原始字符串
* @param conversion 需转换的字符串数组
* @param font 字体
* @param color 颜色
*
* @return 转换好的字符串
*/
- (NSMutableAttributedString *)diverseStringOriginalStr:(NSString *)original conversionStr:(NSString *)conversion withFont:(UIFont *)font withColor:(UIColor *)color
{
original = [NSString nullToString:original];
conversion = [NSString nullToString:conversion];
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:original];
NSRange range = [original rangeOfString:conversion];
[str addAttribute:NSForegroundColorAttributeName value:color range:range];
[str addAttribute:NSFontAttributeName value:font range:range];
return str;
}
swift版
/**
* 多样性字符串处理
*
* @param original 原始字符串
* @param conversion 需转换的字符串
* @param font 字体
* @param color 颜色
*
* @return 转换好的字符串
*/
func diverseStringOriginalStr(original : String,conversionStr conversion : String,withFont font : UIFont,withColor color : UIColor) ->NSMutableAttributedString{
let range : NSRange = (original as NSString).rangeOfString(conversion as String)
let str = NSMutableAttributedString(string: original as String)
str.addAttribute(NSForegroundColorAttributeName, value: color, range: range)
str.addAttribute(NSFontAttributeName, value: font, range: range)
return str
}
/**
* 多样性字符串处理(批量处理)
*
* @param original 原始字符串
* @param conversion 需转换的字符串数组
* @param font 字体
* @param color 颜色
*
* @return 转换好的字符串
*/
func diverseStringOriginalStr(original : String,conversionStrArr conversionArr : NSArray,withFont font : UIFont,withColor color : UIColor) ->NSMutableAttributedString{
let str = NSMutableAttributedString(string: original as String)
for subStr in conversionArr{
let range : NSRange = (original as NSString).rangeOfString(subStr as! String)
str.addAttribute(NSForegroundColorAttributeName, value: color, range: range)
str.addAttribute(NSFontAttributeName, value: font, range: range)
}
return str
}
使用示例
self.contentLabel.attributedText = self.diverseStringOriginalStr("招商银行(尾号1802 储蓄卡)", conversionStr: "(尾号1802 储蓄卡)", withFont: UIFont.systemFontOfSize(14), withColor: UIColor.redColor())
注意:调用此方法前,必须先创建好Label,并且设置好了默认的字体与颜色(就是上图中黑色的字部分)
批量处理的示例
let arr = NSArray(objects: "5,000,","元")
self.moneyLabel.attributedText = DataProcessing.sharedManager.diverseStringOriginalStr("5,000,000元", conversionStrArr: arr, withFont:UIFont.systemFontOfSize(14), withColor: UIColor(red: 255/255.0, green: 93/255.0, blue: 73/255.0, alpha: 1.0))