2016-03-19 14 views
4

NSTextAttachmentイメージをOS XアプリケーションのNSTextViewで動作させることに問題があります。NSTextAttachmentイメージはNSTextViewでは表示されません(しかし、UITextViewでは)?

NSTextAttachmentの画像は表示されません。しかし、それはまだ正しく設定されているようです。 NSTextViewの内容をコピーして、それを再び例えばTextEdit.app、貼り付けられたテキストに画像が正しく含まれています。ここで

は、問題を再現する最小限の遊び場ですので、代わりにココア、それは完璧に動作UIKitに使用して、iOS用

enter image description here

:出力を期待

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 

enter image description here

罰金:

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 

enter image description here

私はどちらの遊び場がhereをダウンロードすることができXCodeの7を使用しています。

どのようなアイデアも大歓迎です。事前に感謝します!

答えて

6
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) 
+0

ありがとうございます。ほんとうにありがとう! – rkusa

関連する問題