问题
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 use
UIColor colorWithPatternImage:for the orange color pattern. Then use that color for the label's
textColor`.
来源:https://stackoverflow.com/questions/13736481/how-to-set-background-image-for-text-into-uilabel