简体   繁体   English

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

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

Can a UILabel display an icon or image? UILabel显示图标或图像吗? ?
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. 我正在制作一个聊天应用程序,它需要在UILabelUITextField显示图标或图像或自定义图像,以便显示一个或多个表达式。 How to do this? 这个怎么做?

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

in iOS >= 7: 在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;

You can do it this way for UILabel 你可以这样对UILabel这样做

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

Or to set image as background to your UITextView 或者将图像设置为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];

You can use iphone emoji . 你可以使用iphone表情符号 Read this Iphone Emoji 阅读此Iphone表情符号

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将UI对象(图像)添加到UITextView或UITextField? - How to add UI Objects (Image) to a UITextView or UITextField? 如何在 UILabel 文本中显示版权图标? - How to display the copyright icon in UILabel text? 如何按方向更改在以编程方式创建的滚动视图中调整以编程方式创建的UILabel,UITextView,UITextfield的大小 - How to resize the programmatically created UILabel,UITextView,UITextfield in programmatically created scroll view as per orientation change 长按UILabel时,是否可以像UITextField(UITextView,UIWebView)一样在UILabel上实现“选择”和“复制”,怎么办? - Can I implement “select” and “copy” on UILabel when it has a long press just as UITextField (UITextView、UIWebView), and how? 如何向 UITextField & UITextView 添加方法? - How to add a method to UITextField & UITextView? 如何自定义UILabel以显示图像? - How can i customize UILabel for image display? 如何获取当前活动的UITextField / UITextView和resignFirstResponder? - How to get the currently active UITextField/UITextView and resignFirstResponder? 如何在没有UITextField或UITextView的情况下启动UIKeyboard? - How to pull up a UIKeyboard without a UITextField or UITextView? 如何在iPhone UITextView或UITextField中更改书写方向 - How to change the writing direction in iPhone UITextView or UITextField 如何在UITextview / UILabel中使用自定义表情? - How to use custom emoticons in UITextview/UILabel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM