gpt4 book ai didi

ios - NSTextAttachment() 额外属性

转载 作者:行者123 更新时间:2023-11-28 23:30:51 25 4
gpt4 key购买 nike

我想在我添加到 UITextView 的一些图像上添加额外信息。该类是NSTextAttachment()。我需要我的图像具有一些 String 描述,以便应用知道图像中的内容。如何添加这个额外的功能?是通过扩展程序实现的吗?

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"

最佳答案

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

  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

关于ios - NSTextAttachment() 额外属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56596970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com