问题描述
使用富文本显示时,出现模糊的问题
问题如下:
我这里只需要3个字特殊颜色:
代码如下:
NSString *colorStr = @"\"上讲台\"";
NSString *originStr = @"为了方便老师上课前检查音视频质量,点击\"上讲台\"仅打开老师的摄像头和麦克风";
NSMutableAttributedString *mutaStr = [[NSMutableAttributedString alloc] initWithString:originStr attributes:[NSDictionary dictionaryWithObjects:@[[NSColor whiteColor]] forKeys:@[NSForegroundColorAttributeName]]];
[mutaStr setAttributes:[NSDictionary dictionaryWithObjects:@[[NSColor colorWithRed:62/255.f green:175/255.f blue:14/255.f alpha:1]] forKeys:@[NSForegroundColorAttributeName]] range:[originStr rangeOfString:colorStr]];
[mutaStr addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"FZY4JW--GB1-0" size:13]
range:NSMakeRange(0, originStr.length)];
解决方案
解决方案是: 设置文本的NSBackgroundColorAttributeName
与背景色一致
NSString *colorStr = @"\"上讲台\"";
NSString *originStr = @"为了方便老师上课前检查音视频质量,点击\"上讲台\"仅打开老师的摄像头和麦克风";
NSMutableAttributedString *mutaStr = [[NSMutableAttributedString alloc] initWithString:originStr attributes:[NSDictionary dictionaryWithObjects:@[[NSColor whiteColor]] forKeys:@[NSForegroundColorAttributeName]]];
[mutaStr setAttributes:[NSDictionary dictionaryWithObjects:@[[NSColor colorWithRed:62/255.f green:175/255.f blue:14/255.f alpha:1]] forKeys:@[NSForegroundColorAttributeName]] range:[originStr rangeOfString:colorStr]];
[mutaStr addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"FZY4JW--GB1-0" size:13]
range:NSMakeRange(0, originStr.length)];
//添加NSBackgroundColorAttributeName
[mutaStr addAttribute:NSBackgroundColorAttributeName value:[NSColor colorWithRed:46/255.0 green:53/255.0 blue:59/255.0 alpha:1]
range:NSMakeRange(0, originStr.length)];