UITextView text background colour

二次信任 提交于 2019-12-11 04:08:30

问题


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?


回答1:


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)];

Then set the attributedText property on your UITextView:

textView.attributedText = attributedString;


来源:https://stackoverflow.com/questions/15438869/uitextview-text-background-colour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!