
如何获得选定范围的方向?
解决方法 iOS版步骤1:声明属性
@property (nonatomic) NSRange lastRange;
步骤2:在TextVIEw代理中,添加:
- (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 <Cocoa/Cocoa.h>@interface TheSelectionizer : NSTextVIEw@property (nonatomic) NSRange lastAnchorPoint;@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;} 我发布了一个项目,以防有人想尝试或想要看到它.
完整的“项目”可用HERE
总结以上是内存溢出为你收集整理的objective-c – 如何获取NSTextView的选择方向?全部内容,希望文章能够帮你解决objective-c – 如何获取NSTextView的选择方向?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)