2017-08-16 11 views
0

私のNSTextViewサブクラスに関する問題があります。私のビューにはカスタム属性を持つ属性付き文字列が含まれているため、カスタム属性がペーストボードにコピーされるように、次のペーストボードメソッドを実装する必要がありました。 readSelectionでNSTextViewカスタム属性のテキストをドラッグ

writeSelection(to:type:) 
readSelection(from:type:) 

、私は手動でペーストボード上の文字列を読み、rangeForUserTextChangeでNSTextViewのtextStorageに書き込みます。私が選択してドラッグテキストがアップライン5からライン1の下に、テキストが正しく行1の下に挿入されたに言うとき

override func readSelection(from pboard: NSPasteboard, type: String) -> Bool { 


    // Manually reads the text on the pasteboard, and write it to textStorage 

    if type == astroBoardAttributedString { 

     if let string = pboard.string(forType: astroBoardAttributedString) { 

      let attrString = NSAttributedString(string: string) 

      self.textStorage?.replaceCharacters(in: self.rangeForUserTextChange, with: attrString) 


      return true 

     } 


    } 

    return super.readSelection(from: pboard, type: type) 

} 

今の問題は、ある、しかしその後、システムは、5行目で使用場所からテキストを削除しようとします長さが変更された後、あなたが見ることができるように余分な行が1行目

enter image description here

/* 
line 1 <-- Drop after this line 
line 2 
line 3 
line 4 
line 5 <-- Drag from this line 

The expected outcome is 

line 1 
line 5 
line 2 
line 3 
line 4 

The actual resulting outcome is 

line 1 
line 5 
line 2 
line 3 
line 5 

What happens is 

line 1 
line 5 <-- Line inserted here (correct) 
line 2 
line 3 
line 4 <-- This line is removed instead :(
line 5 <-- This is the line that should be removed. 
*/ 

の下に追加されましたので、今ライン4を含んでいる、ことを、システムが行を削除しています。ドラッグ&ドロップメソッドがtextviewからドラッグされたtextStorage内のテキストを削除したときに、代行受信メソッドを見つけることができませんでした。

答えて

0

アップルにサポートを依頼した後、私は答えを得ました、ここでは引用しています。

コードは、あなたがペーストボードから読み込まれ、テキストストレージを変更するとき、あなたは矛盾を引き起こしshouldChangeText(...)とdidChangeText()を呼び出すことによって、他のコンポーネントをnotifiyていないことを示していてスニペットテキストストレージとレンダリングの間に

self.shouldChangeText(in:self.rangeForUserTextChange, replacementString: string) 
self.textStorage?.replaceCharacters(in: self.rangeForUserTextChange, with: string) 
self.didChangeText() 
次のようなコードをラップすることで問題を解決できるはずです。