我试图通过NSTextAttachment将图像添加到NSMutableAttributedString,问题是图像似乎不会出现,直到我重新启动应用程序。有任何想法吗?
var attributesForThisSection = [String : Any]()
if key == NSAttachmentAttributeName {
let base64Rep = (value as! String)
if let data = Data(base64Encoded: base64Rep) {
let textAttachment = NSTextAttachment()
if let img = NSImage(data: data) {
textAttachment.image = img
attributesForThisSection[NSAttachmentAttributeName] = textAttachment
}
else {
print("img from data not ok")
}
}
}
然后
let r = NSMakeRange(loc, len)
attributedString?.addAttributes(attributesForThisSection, range: r)
文本显示在NSTextView中,谢谢!
答案 0 :(得分:1)
原来我要做的就是替换
textAttachment.image = img
使用
let attachmentCell = NSTextAttachmentCell(imageCell: img)
textAttachment.attachmentCell = attachmentCell