ios - NSTextAttachment() 额外属性
问题描述
我想在我添加到我的 UITextView
. 班级是NSTextAttachment()
。我需要我的图像有一些 String
描述,以便应用程序知道图像中的内容。如何添加这个额外的功能?是通过一个 Extension
吗?
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_getAssociatedObject
objc_setAssociatedObject
使用时,使用EmojiTextAttachment
代替NSTextAttachment
.
let emoticon:EmojiTextAttachment = EmojiTextAttachment()
let image = UIImage(named: myImage) as UIImage!
emoticon.image = image?.resizeSelf(newWidth, newHeight)
emoticon.emoticonName = emoname
https://developer.apple.com/documentation/objectivec/1418865-objc_getassociatedobject的文档objc_getAssociatedObject
推荐阅读
- ajax - 我怎样才能等到变量可用?
- python - 使用列标题和行值创建新列
- cs50 - 每个 if 公式的输出,CS50 问题集学分
- javascript - 如何将 jQuery 功能与分组元素分组?
- python - Django:通过列表过滤器获取查询集(填充和/或空)
- r - Rcpp 和 boost:它应该可以工作,但不能
- c# - 如何将 ushort 16 位数组转换为图像 C#
- node.js - 带有 Node JS、Express、Heroku 的 Websocket:添加路由会中断 Web 套接字连接
- html - Safari 不支持媒体查询:显示:要显示的表格单元格:块
- apache-kafka - ulimit、lsof、cat /proc/sys/fs/file-max 的区别