How to set background image for text into UIlabel?

白昼怎懂夜的黑 提交于 2019-12-11 01:31:03

问题


We can set color to text in UILabel. But can we set image to text in UILabel? Can you help me? Thanks all!


回答1:


UILabel *lbl  = [[UILabel alloc]initWithFrame:CGRectMake(20, 220, 200, 30)];

[lbl setText:@"Hello"];

[lbl setTextColor:[UIColor yellowColor]];

[lbl setBackgroundColor:[UIColor redColor]];

if you need to set image for your UILabel, you can try like this:

[lbl setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@""]]];

[self.view addSubview:lbl];

If you want your textColor as Image then please do this

[lbl setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"button_select.png"]]];



回答2:


UILabel is only for text. If you want an image, use an UIImageView.




回答3:


If you want to replicate the image you linked to using a UILabel, you can do that by setting the label's text and backgroundColor. Then you useUIColor colorWithPatternImage:for the orange color pattern. Then use that color for the label'stextColor`.



来源:https://stackoverflow.com/questions/13736481/how-to-set-background-image-for-text-into-uilabel

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