Huỳnh Phong
Huỳnh Phong

Reputation: 31

How to set background image for text into UIlabel?

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

Upvotes: 2

Views: 1975

Answers (3)

Manu
Manu

Reputation: 4750

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

Upvotes: 1

rmaddy
rmaddy

Reputation: 318944

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`.

Upvotes: 0

gschandler
gschandler

Reputation: 3208

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

Upvotes: 0

Related Questions