一个简单的小需求,就是在一个字符串里面,指定一部分字节高亮显示,代码如下:
NSString *descStr = @"需要高亮显示的字符";
NSString *nickStr = [NSString stringWithFormat:@"%@ %@",@"这里是高亮显示以外的字符",descStr];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:nickStr];
[attrStr addAttribute:NSForegroundColorAttributeName value:MainColor range:[nickStr rangeOfString:descStr]];
UILabel *nickNameLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headerImg.frame)+5, CGRectGetMaxY(titleLab.frame)+20, KscreenW-65, 20)];
nickNameLab.font = [UIFont systemFontOfSize:15];
nickNameLab.attributedText = attrStr;
[_headerView addSubview:nickNameLab];
效果如下:
作者:稻草人11223