2016-10-15 6 views
0

私はサブクラスNSTextViewを持っています。タブをスペースで置き換えるためのユーザ入力(プリファレンスに基づいて)を変更したいと思います。これまでのところ、私はこのようなものであることをinsertTab方法を変更した:nstextviewペースト中にタブをスペースに置き換えてください。

- (void) insertTab: (id) sender 
{ 
    if(shouldInsertSpaces) { 
     [self insertText: @" "]; 
     return; 
    } 

    [super insertTab: sender]; 
} 

しかし、私はまた、ペーストイベントの間にスペースを交換したいです。私が考えた解決策の1つは、NSTextStorage replaceCharacter:with:メソッドを変更することでしたが、データをtextviewに読み込むと、これがテキストを置き換えることがわかりました。具体的には、ユーザーが手動で入力しているテキストのみを変更したい。

解決策found hereは、ペーストボードを修正することを提案していますが、私はユーザーのペーストボードを台無しにしたくないので、やりたくありません。誰かが私がこれをやり遂げる方法について他の提案をしていますか?

答えて

0

他の質問で述べたように、readSelectionFromPasteboard:type:を見てください。それを上書きしてペーストボードを交換します。例:

- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard type:(NSString *)type { 
    id data = [pboard dataForType:type]; 
    NSDictionary *dictionary = nil; 
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithRTF:data documentAttributes:&dictionary]; 
    for (;;) { 
     NSRange range = [[text string] rangeOfString:@"\t"]; 
     if (range.location == NSNotFound) 
      break; 
     [text replaceCharactersInRange:range withString:@" "]; 
    } 
    data = [text RTFFromRange:NSMakeRange(0, text.length) documentAttributes:dictionary]; 
    NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:@"MyNoTabsPasteBoard"]; 
    [pasteboard clearContents]; 
    [pasteboard declareTypes:@[type] owner:self]; 
    [pasteboard setData:data forType:type]; 
    return [super readSelectionFromPasteboard:pasteboard type:type]; 
} 
関連する問題