如何强调UILabel - IOS中的文本 [英] How to highlight only text in UILabel - IOS
问题描述
我只想突出 UILabel
中的文字,我已经尝试给标签 backgroundColor
是突出的空白空间也看起来不好。所以有没有办法突出文本,而不调整 UILabel
。
I just want to highlight only text in UILabel
, I have tried by giving backgroundColor
for label, but it is highlighting the empty spaces also looks not good. So Is there any way to highlight text without resizing UILabel
.
请检查图片,这里的标签大于文本(中心对齐)
Please check the image, here labels are bigger than the text (center aligned)
Thanx。
推荐答案
考虑跨越多行的文本,但仍然只突出显示文本,并且它们都包含额外的子视图。
Most of the other solutions don't consider text that spans multiple lines while still only highlighting the text, and they are all pretty hacky involving extra subviews.
iOS 6及更高版本的解决方案是使用属性字符串
An iOS 6 and later solution is to use attributed strings:
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:yourString];
[s addAttribute:NSBackgroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(0, s.length)];
label.attributedText = s;
这篇关于如何强调UILabel - IOS中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!