UITextView 文字背景色 [英] UITextView text background colour

查看:57
本文介绍了UITextView 文字背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取透明的 UITextView,并且我知道如何设置它.我还想要的是在已显示文本下方的彩色背景.同样,文本视图背景会将整个视图矩形设置为给定的颜色,我想要的是文本下方的颜色.有什么简单的方法可以实现这样的目标吗?

I am trying to get a transparent UITextView, and I know how to set it. What I also want is like a coloured background right under the text that has been shown. Again the text view background will set the entire view rectangle to a given colour, what I want is colour just under the text. Any easy way of achieving such thing?

推荐答案

据我所知,您正在尝试在文本上实现突出显示效果.如果是这种情况,您可以使用 NSAttributedString.它允许您设置文本的背景颜色,而不是视图背景.

From what I can tell you're trying to accomplish a highlighting effect on the text. If this is the case you could use NSAttributedString. It allows you to set the background color of the text and not the view background.

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test String"];
[attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, 11)];

然后在您的 UITextView 上设置 attributesText 属性:

Then set the attributedText property on your UITextView:

textView.attributedText = attributedString;

这篇关于UITextView 文字背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆