2016-05-15 85 views
1

我正在嘗試創建一個NSAttributedString,其中包含用於OS X應用程序的NSImage如何在NSAttributedString中嵌入圖像?

我已經嘗試了幾種不同的方式,但是這個基本代碼:

let image = NSImage(named: "super-graphic")! 

let attachment = NSTextAttachment() 
attachment.image = image 

let imageString = NSAttributedString(attachment: attachment) 

當我設置上NSLabel,或的NSTextField屬性串,圖像不渲染。

是否可以合併NSImageNSAttributedString以將圖像嵌入到OS X上的屬性字符串中?

回答

0

嗯,我覺得很傻,此代碼的工作:

let image = NSImage(named: "super-graphic")! 
let attachment = NSTextAttachment() 
let cell = NSTextAttachmentCell(imageCell: image) 
attachment.attachmentCell = cell 
let imageString = NSAttributedString(attachment: attachment) 

關鍵的區別是避免使用的NSTextAttachment

的「圖像」屬性