如何根据文本设置标签但我也想要背景颜色 [英] How to set label according to text but i want background color also
问题描述
如何根据text设置UILabel
但我也想要backGroundColor
其中,如果text较小,则显示背景小标签,背景颜色为黑色,文本反之.怎么办?
How to set UILabel
according to text but I want backGroundColor
also in it, if text is small then it will display background small label with background color black and text and vice-versa.How to do?
在图像中,黑色背景颜色是根据动态文本来的.我想要黑色背景颜色,直到文本印度.
In image the black background color came according to the dynamic text. I want black background color till the text India.
推荐答案
让 UILabel 适合所有文本使用 sizeToFit
for UILabel to fit all text use sizeToFit
[label sizeToFit];
如果你想让图片背景的大小作为标签的大小,拿一个imageview
If you want to have pic background make its size as the label size, take an imageview
imageView.frame = CGRectMake(0, 0, label.frame.size.width, label.frame.size.height);
或者如果只想要标签的背景颜色,则使用
or if want only background color of the label then use
[label setBackgroundColor:[UIColor blackColor]];
这篇关于如何根据文本设置标签但我也想要背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!