2016-03-19 4 views
4

Ich habe Probleme, ein NSTextAttachment Image in einer NSTextView für eine OS X-Anwendung zu arbeiten. Das Bild des NSTextAttachment wird einfach nicht angezeigt. Es scheint jedoch immer noch korrekt eingestellt zu sein. Weil, wenn der Inhalt von NSTextView kopiert und in z. TextEdit.app, der eingefügte Text enthält das Bild korrekt.NSTextAttachment Bild nicht in NSTextView (aber in UITextView) angezeigt?

Hier ist ein minimal Spielplatz das Problem reproduzieren:

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

Erwartete Ausgabe:

enter image description here

Für iOS, so UIKit statt Kakao verwenden, es funktioniert perfekt fein:

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

Ich bin mit XCode 7. Beide Spielplätze here heruntergeladen werden können.

Jede Idee ist sehr willkommen, Vielen Dank im Voraus!

Antwort

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

Danke, funktioniert einwandfrei! Ich schätze das wirklich! – rkusa

Verwandte Themen