objective-c – CATextLayer不绘制NSAttributedString背景颜色

objective-c – CATextLayer不绘制NSAttributedString背景颜色,第1张

概述我有CATextLayer,并希望将背景颜色设置为字符串的一部分.但是将背景颜色设置为属性字符串(NSBackgroundColorAttributeName)没有任何效果.正确应用其他属性,例如前景色. NSMutableAttributedString *str = [[[NSMutableAttributedString alloc] initWithString:@"Some Text"] 我有CATextLayer,并希望将背景颜色设置为字符串的一部分.但是将背景颜色设置为属性字符串(NSBackgroundcolorAttributename)没有任何效果.正确应用其他属性,例如前景色.

NSMutableAttributedString *str = [[[NSMutableAttributedString alloc] initWithString:@"Some Text"] autorelease];NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:                            [NScolor yellowcolor],NSForegroundcolorAttributename,[NScolor redcolor],NSBackgroundcolorAttributename,nil];[str setAttributes:attributes range:NSMakeRange(0,3)];textLayer.string = str;

前三个符号以黄色绘制,但背景不会改变.有任何想法吗?

解决方法 我认为CATextLayer类使用CoreText API进行渲染. CoreText API非常低级,仅支持NSAttributedString属性的子集.不幸的是,NSBackgroundcolorAttributename不是其中之一.你必须手动处理它(见 this SO entry). 总结

以上是内存溢出为你收集整理的objective-c – CATextLayer不绘制NSAttributedString背景颜色全部内容,希望文章能够帮你解决objective-c – CATextLayer不绘制NSAttributedString背景颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1001020.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存