如何在uilabel或uitextview中显示图标或图像(uitextfield)

问题描述:

UILabel可以显示图标或图像吗? ?
我正在制作一个聊天应用程序,该应用程序需要在UILabelUITextField中显示图标或图像或自定义图像,以便可以显示表达式或图片。这个怎么做?如何在uilabel或uitextview中显示图标或图像(uitextfield)

您可以UILabel

labelname.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imagename"]]; 

做这种方式或者图像作为背景设置为您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]; 
+0

我想要的文字和图片一起,没有背景。 – user924560

+0

+1对于你Heena,至于正确的答案...我只是看新的问题,只是看到你的答案,并给+1 ... – Sabby

+0

我不想设置其背景图像,我想设置一个图像(与文本在一起,而不是单个)在UILabel或UITextView ..不是背景图像。 – user924560

您可以使用iphone的表情符号。阅读本Iphone Emoji

+0

我想使用自定义图标或图像。 – user924560

+0

谢谢,我尝试工作。 – user924560

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

在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;