截屏2020-11-07 00.19.40.png
1、在一段字符串中,改变多个不一样的字符的大小、颜色
self.normalMemberPriceL.text = @"¥00/元";
self.normalMemberOriginPriceL.text = @"¥000/元";
if (self.normalMemberPriceL.text.length > 3) {
// NSString *headString = @"¥";
// NSString *footString = @"/元";
NSArray *arr = @[@"¥",@"/元"];
// 改变字体颜色
NSMutableAttributedString *normalPriceStr = [[NSMutableAttributedString alloc] initWithString:self.normalMemberPriceL.text];
for (NSString *str in arr) {
// 找出特定字符在整个字符串的位置的位置
NSRange normalRange = NSMakeRange([[normalPriceStr string] rangeOfString:str].location, [[normalPriceStr string] rangeOfString:str].length);
// 修改特定字符的颜色
[normalPriceStr addAttribute:NSForegroundColorAttributeName
value:RGBA(51, 51, 51, 1)
range:normalRange];
// 修改特定字符的字体大小
[normalPriceStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*12 weight:UIFontWeightBold] range:normalRange];
}
self.normalMemberPriceL.attributedText = normalPriceStr;
}
// 划❀中线
if (self.normalMemberOriginPriceL.text.length > 0) {
NSDictionary *attributeDic2 = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attributedStr2 = [[NSMutableAttributedString alloc] initWithString:self.normalMemberOriginPriceL.text attributes:attributeDic2];
self.normalMemberOriginPriceL.attributedText = attributedStr2;
}
/**
2. 只给字符串某一部分添加删除线
如:字符串"123456",只给“456”添加删除线
*/
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"123456"];
[attrStr setAttributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle)}
range:NSMakeRange(3, 3)];
label.attributedText = attrStr;
2、找出特定字符在整个字符串中的位置(所有位置:可能包含多个相同的字符)
self.chineseL.text = [NSString stringWithFormat:@"%@", _cyjsModel.chinese];
self.russianL.text = [NSString stringWithFormat:@"%@", _cyjsModel.russian];
if (self.russianL.text.length > 0 && self.searchChineseStr.length > 0) {
// 改变字体颜色
NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc]initWithString:self.russianL.text];
//找出特定字符在整个字符串中的位置
// NSRange redRange = NSMakeRange([[contentStr string] rangeOfString:self.searchChineseStr].location, [[contentStr string] rangeOfString:self.searchChineseStr].length);
//修改特定字符的颜色
// [contentStr addAttribute:NSForegroundColorAttributeName value:RGBA(247, 55, 66, 1) range:redRange];
//修改特定字符的字体大小
[contentStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:15]
range:[self.russianL.text rangeOfString:self.russianL.text]];
//找出特定字符在整个字符串中的位置(所有位置:可能包含多个相同的字符)
NSMutableArray *locationArr = [self getDuplicateSubStrLocInCompleteStr:self.russianL.text withSubStr:self.searchChineseStr];
for (int i = 0; i<locationArr.count; i++) {
NSNumber *num = [locationArr objectAtIndex:i];
[contentStr addAttribute:NSForegroundColorAttributeName
value:RGBA(247, 55, 66, 1)
range:NSMakeRange(num.integerValue, self.searchChineseStr.length)];
}
self.russianL.attributedText = contentStr;
}
- (NSMutableArray *)getDuplicateSubStrLocInCompleteStr:(NSString *)completeStr withSubStr:(NSString *)subStr
{
NSMutableArray * locMuArr = [[NSMutableArray alloc]init];
if (subStr == nil || subStr == [NSNull null] || [subStr isEqualToString:@""]) {
return locMuArr;
}
NSArray * separatedStrArr = [completeStr componentsSeparatedByString:subStr];
NSInteger index = 0;
for (NSInteger i = 0; i<separatedStrArr.count-1; i++) {
NSString * separatedStr = separatedStrArr[i];
index = index + separatedStr.length;
NSNumber * loc_num = [NSNumber numberWithInteger:index];
[locMuArr addObject:loc_num];
index = index+subStr.length;
}
return locMuArr;
}
也可以单独判断一段字符串的每一个字符,判断其是否是自己想要改变大小、颜色的字符:
// 判断字符串的每一个字符,判断其是否为“/”,改变其字体大小
for (int i = 0; i < self.rightMarkL.text.length; i++) {
NSRange range = {i, 1};// 范围是起点和长度,而非起点和终点
NSString * subStr = [self.rightMarkL.text substringWithRange:range];
if ([subStr isEqualToString:@"/"]) {
// 在这里单独设置"/"的大小、颜色或其他
}
}
例如:cell 中:(首先将全部字体改边颜色,在改局部字体的颜色)
self.contentL.text = [NSString stringWithFormat:@"%@", _replyModel.comment_text];
if ([[NSString stringWithFormat:@"%@", _replyModel.comment_text] hasPrefix:@"@"]) { // 回复
NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc] initWithString:self.contentL.text];
// 找到“@**”
NSRange range = NSMakeRange([[contentStr string] rangeOfString:[NSString stringWithFormat:@"@%@", _replyModel.reply_nick_name]].location, [[contentStr string] rangeOfString:[NSString stringWithFormat:@"@%@", _replyModel.reply_nick_name]].length);
// 改变特定字符的颜色和字体(***首先将全部字体改边颜色,在改局部字体的颜色***)
[contentStr addAttribute:NSForegroundColorAttributeName value:RGBA(0, 0, 0, 1) range:NSMakeRange(0, self.contentL.text.length)];
[contentStr addAttribute:NSForegroundColorAttributeName value:RGBA(149, 150, 154, 1) range:range];
[contentStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*14 weight:UIFontWeightMedium] range:range];
self.contentL.attributedText = contentStr;
} else { // 没回复
NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc] initWithString:self.contentL.text];
// 改变特定字符的颜色和字体
[contentStr addAttribute:NSForegroundColorAttributeName value:RGBA(0, 0, 0, 1) range:NSMakeRange(0, self.contentL.text.length)];
[contentStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*13 weight:UIFontWeightMedium] range:NSMakeRange(0, self.contentL.text.length)];
self.contentL.attributedText = contentStr;
}
3.
截屏2021-06-02 18.26.24.png
if (self.contentL.text.length > 0) {
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
NSArray*fixArray = [self.contentL.text componentsSeparatedByString:@"|"];//从字符"|"中分隔成2个
NSString *fixStr = @"";
if (fixArray.count > 0) {
fixStr = [NSString stringWithFormat:@"%@", fixArray[0]]; // 拿到“|”之前的文字
[arr addObject:fixStr];
}
[arr addObjectsFromArray:@[@"|",@"/",]];
// 改变字体颜色
NSMutableAttributedString *normalPriceStr = [[NSMutableAttributedString alloc] initWithString:self.contentL.text];
for (NSString *str in arr) {
// 找出特定字符在整个字符串的位置的位置
NSRange normalRange = NSMakeRange([[normalPriceStr string] rangeOfString:str].location, [[normalPriceStr string] rangeOfString:str].length);
if ([str isEqualToString:fixStr]) {
// 第一个元素变色,其他两个变其他颜色
[normalPriceStr addAttribute:NSForegroundColorAttributeName
value:RGBA(257, 76, 57, 1)
range:normalRange];
} else {
[normalPriceStr addAttribute:NSForegroundColorAttributeName
value:RGBA(153, 153, 153, 1)
range:normalRange];
}
}
self.contentL.attributedText = normalPriceStr;
}
4、字符串中的数字全部改变大小和颜色:
if (self.statisticsTipsL.text.length > 0) {
NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc]initWithString:self.statisticsTipsL.text];
[contentStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:12]
range:[self.statisticsTipsL.text rangeOfString:self.statisticsTipsL.text]];
[contentStr addAttribute:NSForegroundColorAttributeName value:RGBA(238, 197, 116, 1) range:[self.statisticsTipsL.text rangeOfString:self.statisticsTipsL.text]];
for (int i = 0; i < self.statisticsTipsL.text.length; i++) {
NSRange range = {i, 1};// 范围是起点和长度,而非起点和终点
NSString * subStr = [self.statisticsTipsL.text substringWithRange:range];
if ([subStr isStringContainNumberWith:subStr]) { // 判断是否是数字,改变数字的大小和颜色
[contentStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*20 weight:UIFontWeightBold]
range:range];
[contentStr addAttribute:NSForegroundColorAttributeName value:RGBA(193, 162, 122, 1) range:range];
}
}
self.statisticsTipsL.attributedText = contentStr;
}
// 判断某一个字符是否是数字:
/** 判断:字符串是否包含数字 */
- (BOOL)isStringContainNumberWith:(NSString *)str {
NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];
//count是str中包含[0-9]数字的个数,只要count>0,说明str中包含数字
if (count > 0) {
return YES;
}
return NO;
}
文本行距:
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineBreakMode:NSLineBreakByCharWrapping];
style.lineSpacing = 5;
// 设置行间距:
NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:self.contentL.text];
[attriString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [self.contentL.text length])];
for (NSString *str in changeColors) {
// 找出特定字符在整个字符串 第一次 出现的位置:
NSRange normalRange = [[attriString string] rangeOfString:str];
// 修改特定字符的颜色
[attriString addAttribute:NSForegroundColorAttributeName
value:RGBA(223, 109, 53, 1)
range:normalRange];
}
self.contentL.attributedText = attriString;
一般使用:
if (self.tipsL.text > 0) {
NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc]initWithString:self.tipsL.text];
[contentStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*17 weight:UIFontWeightMedium]
range:[self.tipsL.text rangeOfString:self.tipsL.text]];
[contentStr addAttribute:NSForegroundColorAttributeName value:RGBA(0, 0, 0, 1) range:[self.tipsL.text rangeOfString:self.tipsL.text]];
NSRange redRange = NSMakeRange([[contentStr string] rangeOfString:@"6"].location, [[contentStr string] rangeOfString:@"6"].length);
[contentStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*21 weight:UIFontWeightMedium]
range:redRange];
[contentStr addAttribute:NSForegroundColorAttributeName value:RGBA(238, 197, 116, 1) range:redRange];
self.tipsL.attributedText = contentStr;
}