繁体   English   中英

如何获取NSTextView的选择方向?

[英]How can I get the selection direction of an NSTextView?

我正在尝试在NSTextView获取所选范围的NSTextView 换句话说,当您使用shift+leftarrowshift+rightarrow时,所选范围是否会更改其位置或长度。 我的第一个问题是selectionAffinity表示方向,但它似乎只适用于多行选择。

如何获得所选范围的方向?

iOS版

第1步:声明属性

@property (nonatomic) NSRange lastRange;

第2步:在TextView Delegate中,添加以下内容:

- (void) textViewDidChangeSelection:(UITextView *)textView

    NSRange currentRange = NSMakeRange(textView.selectedRange.location, textView.selectedRange.length);

    if (currentRange.length == 0) {
        NSLog(@"Nothing selected");
        _lastRange = currentRange;
    }
    else {
        if (currentRange.location < _lastRange.location) {
            NSLog(@"Selecting LEFT");
        }
        else {
            NSLog(@"Selecting RIGHT");
        }
    }
}

OSX

OSX具有所需的所有功能,需要更多的工作,但这就是我提出的一个可行的,一致的解决方案。 重命名是可选的,或者更确切地说......鼓励......

第1步:对NSTextView进行子类化

在你的SubClass.h

#import "TheSelectionizer.h"

- (void)setSelectedRange:(NSRange)charRange affinity:(NSSelectionAffinity)affinity stillSelecting:(BOOL)stillSelectingFlag {

    if (charRange.length == 0) {
        _lastAnchorPoint = charRange;
    }
    [super setSelectedRange:charRange affinity:affinity stillSelecting:stillSelectingFlag];
}

@end

在你的SubClass.m

 #import "TheSelectionizer.h" - (void)setSelectedRange:(NSRange)charRange affinity:(NSSelectionAffinity)affinity stillSelecting:(BOOL)stillSelectingFlag { if (charRange.length == 0) { _lastAnchorPoint = charRange; } [super setSelectedRange:charRange affinity:affinity stillSelecting:stillSelectingFlag]; } @end 
第2步:实现NSTextViewDelegate
 - (NSRange) textView:(NSTextView *)textView willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange toCharacterRange:(NSRange)newSelectedCharRange { if (newSelectedCharRange.length != 0) { TheSelectionizer * selectionizer = (TheSelectionizer *)textView; int anchorStart = (int)selectionizer.lastAnchorPoint.location; int selectionStart = (int)newSelectedCharRange.location; int selectionLength = (int)newSelectedCharRange.length; /* If mouse selects left, and then a user arrows right, or the opposite, anchor point flips. */ int difference = anchorStart - selectionStart; if (difference > 0 && difference != selectionLength) { if (oldSelectedCharRange.location == newSelectedCharRange.location) { // We were selecting left via mouse, but now we are selecting to the right via arrows anchorStart = selectionStart; } else { // We were selecting right via mouse, but now we are selecting to the left via arrows anchorStart = selectionStart + selectionLength; } selectionizer.lastAnchorPoint = NSMakeRange(anchorStart, 0); } // Evaluate Selection Direction if (anchorStart == selectionStart) { if (oldSelectedCharRange.length < newSelectedCharRange.length) { // Bigger NSLog(@"Will select right in overall right selection"); } else { // Smaller NSLog(@"Will select left in overall right selection"); } } else { if (oldSelectedCharRange.length < newSelectedCharRange.length) { // Bigger NSLog(@"Will select left in overall left selection"); } else { // Smaller NSLog(@"Will select right in overall left selection"); } } } return newSelectedCharRange; } 

我发布了一个项目,万一有人想尝试或想看到它。

这里有完整的“项目”

假设声明了NSRange yourPreviousRange。

//Check when NSTextView changed its value
@interface delegateAppDelegate : NSObject <NSApplicationDelegate, NSTextViewDelegate> {
    NSWindow *window;
}

-(void)textDidChange:(NSNotification *)notification {
    NSRange yourRange = notification.object.selectedRange;
    if (yourRange.location == yourPreviousRange.location - 1 || yourRange.length == yourPreviousRange.length){
     //left direction  
    } else if (yourRange.location == yourPreviousRange.location || yourRange.length == yourPreviousRange.length + 1){
    //right direction
    }
    yourPreviousRange = yourRange;
}

使用NSTextViewDelegate方法: -textView:willChangeSelectionFromCharacterRange:toCharacterRange:-textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:如果你想要多选择支持。

请注意,如果您只实现-textView:willChangeSelectionFromCharacterRange:toCharacterRange:将禁止多选。

例如,没有多选:

- (NSRange)textView:(NSTextView *)aTextView willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange toCharacterRange:(NSRange)newSelectedCharRange
{
    if (newSelectedCharRange.length == 0 && oldSelectedCharRange.length == 0) 
    {
        /* move the insertion point */

        if (newSelectedCharRange.location < oldSelectedCharRange.location)
            NSLog(@"insertion point move left");
        else
            NSLog(@"insertion point move right");
    }
    else if (newSelectedCharRange.length == 0 && oldSelectedCharRange.length != 0)
    {
        /* end a selection and move insertion point */

        if (newSelectedCharRange.location == oldSelectedCharRange.location)
            NSLog(@"insertion point at start from previous selection");
        else if (newSelectedCharRange.location < oldSelectedCharRange.location)
            NSLog(@"insertion point move left");
        else
            NSLog(@"insertion point move right");
    }
    else if (oldSelectedCharRange.length == 0 && newSelectedCharRange.length != 0)
    {
        /* start a selection */

        if (newSelectedCharRange.location == oldSelectedCharRange.location)
            NSLog(@"start a selection at insertion point, move right");
        else if (newSelectedCharRange.location < oldSelectedCharRange.location)
            NSLog(@"start a selection, move left");
        else
            NSLog(@"start a selection, move right");
    }
    else
    {
        if (newSelectedCharRange.location < oldSelectedCharRange.location)
            NSLog(@"selection move left");
        else
            NSLog(@"selection move right");
    }

    return newSelectedCharRange;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM