Mac开发-NSAttributedString颜色模糊问题记录

问题描述

使用富文本显示时,出现模糊的问题
问题如下:
在这里插入图片描述
我这里只需要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)];

最终效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值