像我们在 uitextfield 中插入表情符号(unicodes)一样插入图像

Posted

技术标签:

【中文标题】像我们在 uitextfield 中插入表情符号(unicodes)一样插入图像【英文标题】:Inserting image like we insert emojis(unicodes) in uitextfield 【发布时间】:2017-03-27 05:46:27 【问题描述】:

我有一个很奇怪的要求。自定义图像需要在 UILabel 中插入文本。我的第一个问题是这在 UILabel 中是可能的。我可以看到表情符号可以安装在那里,因为它们是 unicode,但是自定义图像是纯图像,所以如何同时添加图像和文本。

STextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"myimage.png"];
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:@"My label text"];
[myString appendAttributedString:attachmentString];
myLabel.attributedText = myString;

我这样做了,但是这并没有在运行时发生,就像我想让用户选择同时使用图标/贴纸和文本一样。

【问题讨论】:

我认为你可以用 UITextView 做到这一点,check here 可以在UILabel中使用NSTextAttachment ***.com/a/19320626/742298 @Anbu.Karthik NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:@"myimage.png"]; NSAttributedString *attachmentString = [NSAttributedString attributesStringWithAttachment:attachment]; NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"我的标签文本"]; [myString appendAttributedString:attachmentString]; myLabel.attributedText = myString;我这样做了,但是这并没有在运行时发生,就像我想让用户选择同时使用图标/贴纸和文本一样。 当你想要这个时I want to give user option to use icon/sticker and text simultaneously 你的代码没问题 【参考方案1】:

创建一个集合视图并将其作为您的 textView 或 TextField 的输入视图。在从集合视图中选择元素时,您可以将图像传递给您的函数,就像您的代码 sn-p 一样,它会正常工作。这就是在基于聊天的应用程序中通常添加贴纸的方式。希望这可以帮助。如有任何问题,请与我们联系。快乐的编码。

【讨论】:

谢谢你..我只是这样做..在图像和文本分离之前它工作得很好但是如果用户想要发送 textimagetextimage 等问题就会出现问题。在这里做什么...

以上是关于像我们在 uitextfield 中插入表情符号(unicodes)一样插入图像的主要内容,如果未能解决你的问题,请参考以下文章

打开表情符号键盘时自定义 UITextField 冻结应用程序?

emoji纸飞机表情符号怎么输入

在 UITextField 与 UIViewRepresentable 和 SwifUI 之间绑定文本时出现问题

在 React Native 的 TextInput 中插入自定义表情符号图像

苹果emoji表情符号 那种特殊的符号怎么打出来

更改 UITextField 和 UITextView 光标/插入符号颜色