Reputation: 11
Can a UILabel
display an icon or image? ?
I was making a chat application, which needs to show icon or image or custom image in a UILabel
or UITextField
so that an expression or pictures can be displayed. How to do this?
Upvotes: 1
Views: 3847
Reputation: 1303
in iOS >= 7:
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"icon.png"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"text bla bla bla"];
[myString appendAttributedString:attachmentString];
self.label.attributedText = myString;
Upvotes: 1
Reputation: 13180
UITextView *textView = [[UITextView alloc]initWithFrame: self.view.frame];
textView.text = @"your text";
UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
imgView.image = [UIImage imageNamed: @"yourImage.png"];
[self.view addSubview: imgView];
[imgView addSubview: imgView];
Upvotes: 3
Reputation: 754
You can do it this way for UILabel
labelname.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imagename"]];
Or to set image as background to your UITextView
UITextView *textView = [[UITextView alloc]initWithFrame: window.frame];
textView.text = @"text\n text\n text";
UIImageView *imgView = [[UIImageView alloc]initWithFrame: textView.frame];
imgView.image = [UIImage imageNamed: @"myImage.jpg"];
[textView addSubview: imgView];
[textView sendSubviewToBack: imgView];
[window addSubview: textView];
Upvotes: 0