首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSTextAttachment()附加属性

NSTextAttachment()附加属性
EN

Stack Overflow用户
提问于 2019-06-14 19:09:44
回答 1查看 91关注 0票数 0

我想在我添加到UITextView中的一些图片上添加额外的信息。这个类是NSTextAttachment()。我需要我的图像有一些String的描述,这样应用程序就知道图像里面是什么。我如何添加这个额外的功能?是通过Extension实现的吗?

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

发布于 2019-09-16 13:30:14

代码语言:javascript
复制
class EmojiTextAttachment:NSTextAttachment{    
}
private var EmotagAssociationKey:String? = nil
extension EmojiTextAttachment{

    var emoticonName:String?{
        get {return objc_getAssociatedObject(self,&EmotagAssociationKey) as? String ?? ""}
        set {objc_setAssociatedObject(self, &EmotagAssociationKey, newValue, .OBJC_ASSOCIATION_RETAIN) }
    }
}

已经有一段时间了,但我的解决方案已经出来了。扩展NSTextAttachment您可以使用objc_getAssociatedObjectobjc_setAssociatedObject添加参数

在使用时,请使用EmojiTextAttachment而不是NSTextAttachment

代码语言:javascript
复制
  let emoticon:EmojiTextAttachment = EmojiTextAttachment()
                    let image = UIImage(named: myImage) as UIImage!
                    emoticon.image = image?.resizeSelf(newWidth, newHeight)
                    emoticon.emoticonName = emoname

objc_getAssociatedObject https://developer.apple.com/documentation/objectivec/1418865-objc_getassociatedobject的文档

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56596970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档