1、工作中我们可能会遇到下面这种情况
5BBA405D-4896-46B3-B1D6-BF8554BB1F2F.png
- 我的思路是字符串中,将需要变色变字体的用
[]
包起来,然后使用正则将[]
提取出来进行修改
- 比如上面的字符串我是这样写的
@"123[哈哈]456[呵呵]789[666]"
2、思路有了,接下来就直接上代码
/**
获取text中[]的字符串
并且将所有[]的range存储到array
*/
+ (NSString *)searchTextRangeWithArray:(NSMutableArray*)array text:(NSString*)text regexString:(NSString*)regexString{
NSRange range = [text rangeOfString:regexString options:NSRegularExpressionSearch];
if (range.location != NSNotFound) {
//包装字典,存进数组
// 我是[一句话]啊,真的是[一句]话,不骗你,哦,是吗?不[骗我]
// 我是一句话啊,真的是[一句]话,不骗你,哦,是吗?不[骗我]
// 我是一句话啊,真的是一句话,不骗你,哦,是吗?不[骗我]
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSString *aStr = [text substringWithRange:range]; // [一句话] [一句] [骗我]
aStr = [aStr stringByReplacingOccurrencesOfString:@"[" withString:@""];
aStr = [aStr stringByReplacingOccurrencesOfString:@"]" withString:@""];
// aStr = 一句话
// aStr = 一句
// aStr = 骗我
// 去除[] 我是一句话啊,真的是[一句]话,不骗你,哦,是吗?不[骗我]
// 去除[] 我是一句话啊,真的是一句话,不骗你,哦,是吗?不[骗我]
// 去除[] 我是一句话啊,真的是一句话,不骗你,哦,是吗?不骗我
text = [text stringByReplacingOccurrencesOfString:[text substringWithRange:range] withString:aStr];
NSRange newRange = NSMakeRange(range.location, aStr.length);
dict[@"subStringRange"] = [NSValue valueWithRange:newRange];
[array addObject:dict];
return [self searchTextRangeWithArray:array text:text regexString:regexString];
}
return text;
}
+ (NSMutableAttributedString *)setupAttributeWithString:(nullable NSString *)string colors:(nullable NSArray *)colors fonts:(nullable NSArray *)fonts {
if ([GGUtil stringIsEmpty:string]) { return nil; }
NSMutableArray *arrayM = [NSMutableArray array];
NSString *regexString = @"(\\[[^\\]]*\\])";
string = [GGUtil searchTextRangeWithArray:arrayM text:string regexString:regexString];
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
for (NSInteger i = 0; i < arrayM.count; i++) {
NSDictionary *dic = arrayM[i];
NSRange subStringRange = [dic[@"subStringRange"] rangeValue];
if (i > colors.count - 1) {
[attri addAttributes:@{NSForegroundColorAttributeName:[colors lastObject]} range:subStringRange];
}else {
[attri addAttributes:@{NSForegroundColorAttributeName:colors[i]} range:subStringRange];
}
if (i > fonts.count - 1) {
[attri addAttributes:@{NSFontAttributeName:[fonts lastObject]} range:subStringRange];
}else {
[attri addAttributes:@{NSFontAttributeName:fonts[i]} range:subStringRange];
}
}
return attri;
}
3、如何使用
self.titleLbl.attributedText =
[LAHelper setupAttributeWithString:@"123[哈哈]456[呵呵]789[666]"
colors:@[[UIColor redColor],[UIColor greenColor],[UIColor blueColor]]
fonts:@[[UIFont boldSystemFontOfSize:17],[UIFont systemFontOfSize:10],[UIFont systemFontOfSize:25]]];
4、有些是要设置行间距啥的,可以再进行包装一下
/**
设置string带有中括号的文字
返回attributedString
*/
+ (NSMutableAttributedString *)setupAttributeWithString:(nullable NSString *)string colors:(nullable NSArray *)colors fonts:(nullable NSArray *)fonts lineSpacing:(CGFloat)lineSpacing alignment:(NSTextAlignment)alignment lineBreakMode:(NSLineBreakMode)lineBreakMode {
NSMutableAttributedString *attri = [GGUtil setupAttributeWithString:string colors:colors fonts:fonts];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = lineSpacing;
style.alignment = alignment;
style.lineBreakMode = lineBreakMode;
[attri addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attri.length)];
return attri;
}