【发布时间】:2016-03-19 18:56:21
【问题描述】:
我在获取 NSTextAttachment 图像以在 OS X 应用程序的 NSTextView 中工作时遇到问题。
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。
非常欢迎任何想法,提前致谢!
【问题讨论】: