2016-09-08 10 views
2

私はNSTextViewを使用し、画像NSTextAttachmentを挿入しています。それを選択してコピーして貼り付けると、貼り付けられたイメージがtextviewに表示されますが、NSTextView.attributedStringからコンテンツ文字列を取得できません。どうして ?NSTextViewコピー貼り付けNSTextAttachment on Mac os

+0

ねえ、解決策はまだ見つかりましたか? – Daniel

+1

これを修正するには、カスタムタイプを使用してください:newContent = content.replace(String(c)、new: ""、mode:.literal) NSPasteboard.general()。addTypes([CustomParsteFormat.Emotion]、所有者:nil) NSPasteboard.general()。setString(newContent、forType:CustomParsteFormat.Emotion) – user1461382

答えて

1

私も同様の問題がありました。確かにあなたが望むものであるかどうかは分かりませんが、NSTextAttachmentの文字列表現をコピーして貼り付けることができるようにしたかったのです。私もそれが文字列表現だ覚えカスタムNSTextAttachmentCellクラスを使用しています

override func writeSelection(to pboard: NSPasteboard, type: String) -> Bool { 
    let selectedAttributedString = attributedString().attributedSubstring(from: selectedRange()) 
    let selectedAttributedStringCopy = NSMutableAttributedString(attributedString: selectedAttributedString) 

    selectedAttributedStringCopy.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0,(selectedAttributedString.string.characters.count)), options: .reverse, using: {(_ value: Any?, _ range: NSRange, _ stop: UnsafeMutablePointer<ObjCBool>) -> Void in 

     if let textAttachment = value as? NSTextAttachment, let textAttachmentCell = textAttachment.attachmentCell as? YouCustomTextAttachmentCellClass { 
      var range2: NSRange = NSRange(location: 0,length: 0) 
      let attributes = selectedAttributedStringCopy.attributes(at: range.location, effectiveRange: &range2) 

      selectedAttributedStringCopy.replaceCharacters(in: range, with: NSMutableAttributedString(string: textAttachmentCell.yourStringRepresentation)) 
      selectedAttributedStringCopy.addAttributes(attributes, range: range) 

      // selectedAttributedStringCopy.insert(attachmentAttributedString, at: range.location) 

     } 
    }) 

    pboard.clearContents() 
    pboard.writeObjects([selectedAttributedStringCopy]) 

    return true 
} 

注:

は、私は私のカスタムNSTextViewクラスでfunc writeSelection(to pboard: NSPasteboard, type: String) -> Boolをオーバーライドしてしまいました。

関連する問題