user924560
user924560

Reputation: 11

how to display an icon or image in a uilabel or uitextview(uitextfield)

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

Answers (4)

Leonardo Cavalcante
Leonardo Cavalcante

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

PJR
PJR

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

Bonny
Bonny

Reputation: 2038

You can use iphone emoji. Read this Iphone Emoji

Upvotes: 0

Heena
Heena

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

Related Questions