简体   繁体   中英

How to put an image and write text in same UILabel?

I want to put an image in my UILabel which should be right aligned and write some text in the same UILabel which should be left aligned. How can i do that programatically? Thanks in advance

Set that image as background image

theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blah"]];

and write text as

thelabel.text =  @"abc"

set the align of the label

[thelabel setTextAlignment:UITextAlignmentLeft];

I am not sure that you can actually put an image inside a UILabel, but if you can, you'll be using the addSubView: method.

UILabel *label = ....
label.text = @"Hi there";

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,width,height)];
[label addSubView:image];

Try it out and see if it works, if not, then you'll probably have to set the image's frame to a frame that floats along with the label's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM