我在获取时遇到问题NSTextAttachment
工作图像NSTextView
对于 OS X 应用程序。
的形象NSTextAttachment
只是根本不显示。然而,它似乎仍然设置正确。因为在复制内容的时候NSTextView
并将其粘贴回例如TextEdit.app,粘贴的文本正确包含图像。
这是一个重现该问题的最小游乐场:
import Cocoa
let img = NSImage(named: "Checked")
let textView = NSTextView(frame: NSMakeRect(0, 0, 254, 64))
let attrstr = NSMutableAttributedString(string: "Test")
let attch = NSTextAttachment()
attch.image = img
attrstr.appendAttributedString(NSAttributedString(attachment: attch))
textView.textStorage!.setAttributedString(attrstr)
textView
预期输出:
对于 iOS,因此使用 UIKit 而不是 Cocoa,它工作得很好:
import UIKit
let img = UIImage(named: "Checked")
let textView = UITextView(frame: CGRectMake(0.0, 0.0, 200.0, 44.0))
let attrstr = NSMutableAttributedString(string: "Test")
let attch = NSTextAttachment()
attch.image = img
attrstr.appendAttributedString(NSAttributedString(attachment: attch))
textView.attributedText = attrstr
textView
我使用的是XCode 7。两个playground都可以下载here https://dl.dropboxusercontent.com/u/6699613/nstextattachmentplayground.zip.
非常欢迎任何想法,提前致谢!
var thumbnailImage: NSImage? = // some image
var attachmentCell: NSTextAttachmentCell = NSTextAttachmentCell.initImageCell(thumbnailImage!)
var attachment: NSTextAttachment = NSTextAttachment()
attachment.attachmentCell = attachmentCell
var attrString: NSAttributedString = NSAttributedString.attributedStringWithAttachment(attachment)
self.textView.textStorage().appendAttributedString(attrString)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)