简体   繁体   English

如何获取NSTextView的选择方向?

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

I'm trying to get the direction of the selected ranges in an NSTextView . 我正在尝试在NSTextView获取所选范围的NSTextView In other words, whether the selected ranges change their location or length when you use shift+leftarrow and shift+rightarrow . 换句话说,当您使用shift+leftarrowshift+rightarrow时,所选范围是否会更改其位置或长度。 My first though was that selectionAffinity represents the direction, but it only seems to apply to multi-line selections. 我的第一个问题是selectionAffinity表示方向,但它似乎只适用于多行选择。

How can I get the direction of selected ranges? 如何获得所选范围的方向?

iOS iOS版

Step 1: Declare Property 第1步:声明属性

@property (nonatomic) NSRange lastRange;

Step 2: In TextView Delegate, add this: 第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

OSX with all the features requested involved a little bit more work, but here's what I came up with for a working, consistent solution. OSX具有所需的所有功能,需要更多的工作,但这就是我提出的一个可行的,一致的解决方案。 Renaming is optional, or rather ... encouraged ... 重命名是可选的,或者更确切地说......鼓励......

Step 1: Subclass a NSTextView 第1步:对NSTextView进行子类化

In your SubClass.h : 在你的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

In your SubClass.m 在你的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 
Step 2: Implement NSTextViewDelegate 第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; } 

I posted a project just in case someone wants to try it or wants to see it. 我发布了一个项目,万一有人想尝试或想看到它。

Full "project" available HERE 这里有完整的“项目”

Assumed that NSRange yourPreviousRange is declared. 假设声明了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;
}

Use NSTextViewDelegate methods : -textView:willChangeSelectionFromCharacterRange:toCharacterRange: or -textView:willChangeSelectionFromCharacterRanges:toCharacterRanges: if you want multi-selections support. 使用NSTextViewDelegate方法: -textView:willChangeSelectionFromCharacterRange:toCharacterRange:-textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:如果你想要多选择支持。

Note that if you only implements -textView:willChangeSelectionFromCharacterRange:toCharacterRange: the multi-selections will be disallowed. 请注意,如果您只实现-textView:willChangeSelectionFromCharacterRange:toCharacterRange:将禁止多选。

For example without multi-selections : 例如,没有多选:

- (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