Ios NSTextAttachment()附加属性
我想在添加到我的Ios NSTextAttachment()附加属性,ios,swift,uitextview,Ios,Swift,Uitextview,我想在添加到我的UITextView的一些图像上添加额外的信息。该类是NSTextAttachment()。我需要我的图像有一些字符串描述,以便应用程序知道图像中的内容。如何添加此额外功能?是否通过分机 let attachment = NSTextAttachment() attachment.image = image attachment.bounds = CGRect(origin: .zero, size: image.size) // Extra properties wanted
UITextView
的一些图像上添加额外的信息。该类是NSTextAttachment()
。我需要我的图像有一些字符串
描述,以便应用程序知道图像中的内容。如何添加此额外功能?是否通过分机
let attachment = NSTextAttachment()
attachment.image = image
attachment.bounds = CGRect(origin: .zero, size: image.size)
// Extra properties wanted
attachment.description = "add in info about what is in attachment"
attachment.moreInfo = "some more info about the attachment"
已经有一段时间了,但我的解决方案已经出来了。
扩展NSTextAttachment
您可以使用objc\u getAssociatedObject
和objc\u setAssociatedObject
使用它时,请使用EmojiTextAttachment
而不是NSTextAttachment
let emoticon:EmojiTextAttachment = EmojiTextAttachment()
let image = UIImage(named: myImage) as UIImage!
emoticon.image = image?.resizeSelf(newWidth, newHeight)
emoticon.emoticonName = emoname
objc\u getAssociatedObject
的文档通过属性字符串本身的自定义属性,而不是通过文本附件添加您的信息。@matt您能给我举个例子吗?我一直在努力,真的不知道如何做到这一点…我不知道你的意思。您只能使用NSTextAttachment,因为您有一个NSMutableAttributeString。好的,让您添加属性到属性字符串的任何延伸。没有法律规定必须使用布局引擎知道的属性。您可以有一个description
属性和一个moreInfo
属性,并给它们任何您喜欢的值。现在,您已经将自定义信息存储在字符串中所需的位置。我需要这些属性,以便以后可以调用它们。例如,图像1的描述是什么。我不明白你是怎么说的?我无法将扩展名添加到实际附件,只能将信息添加到范围?你是这么说的吗?
let emoticon:EmojiTextAttachment = EmojiTextAttachment()
let image = UIImage(named: myImage) as UIImage!
emoticon.image = image?.resizeSelf(newWidth, newHeight)
emoticon.emoticonName = emoname