取消选择 NSTextField 中的文本
Deselecting Text in NSTextField
我正在开发一个新应用程序,我 运行 遇到了此处描述的相同问题,以防止取消选择输入到 nstextfield 中的文本:http://www.cocoabuilder.com/archive/cocoa/195313-nstextfield-how-to-deselect-text.html
有很多关于使用 NSTextViews 执行此操作的问题,但我无法为 NSTextFields 找到有效的答案。
在我的项目中,我有一个 window,里面有一个文本字段和一个控制器 class,它也是文本字段的委托。我有一个 IBAction 用于在输入时发送的文本字段,它根据字段中的文本执行操作,以及:
(void)controlTextDidChange:(NSNotification *)note
和
(BOOL)control:(NSControl *)control
textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
它处理一些自定义自动完成,如对 Suppressing the text completion dropdown for an NSTextField
的回答
我的问题在于,当我按回车键提交输入的文本时,字段中的字符串被完全选中,但我希望能够取消选择文本并将插入点放在末尾,如前所述在第一个 link.
我可以在多个地方取消选择文本,但实际上取消选择不起作用。我已经尝试按照第一个 link 中的描述获取字段编辑器,我也尝试使用 controlTextDidEndEditing: 方法,因为我确实在上面的 controlTextDidChange: 方法中获得了一个字段编辑器:
- (void)controlTextDidEndEditing:(NSNotification *)note{
NSTextView *textView = [[note userInfo] objectForKey:@"NSFieldEditor"];
[textView setSelectedRange:NSMakeRange([[self.ParseField stringValue]length]-1, 0)
affinity:NSSelectionAffinityUpstream
stillSelecting:NO];
}
我也试过在该字段上禁用和重新启用编辑,但这也没有用。
像能够向文本字段发送 moveDown: 消息这样简单的事情对我有用,因为它与按下向下箭头相同,但文本字段无法识别该选择器。 (我以为 NSTextField 继承自 NSResponder,所以它会起作用,但我猜不是?)
感谢您的帮助
在一个只有 window 和文本字段的简单应用程序中。
我将文本字段设置为 firstResponder。
这使得文本在应用 运行s 时被选中。
如果我添加到 applicationDidFinishLaunching
NSRange tRange = [[ _theTextField currentEditor] selectedRange];
[[ _theTextField currentEditor] setSelectedRange:NSMakeRange(tRange.length,0)];
现在 运行 未选中文本且插入点位于文本末尾时。
我使用 selectedRange 来获取选择字符长度。由于文本是一开始就选中的,看起来比较简单。
- (void)awakeFromNib{
[_theTextField setStringValue:@"100000"];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[[[NSApplication sharedApplication] mainWindow] makeFirstResponder:_theTextField];
NSRange tRange = [[ _theTextField currentEditor] selectedRange];
[[ _theTextField currentEditor] setSelectedRange:NSMakeRange(tRange.length,0)];
}
我一直在寻找 Swift 3 中的解决方案,所以这对我有用:
theTextField.currentEditor()?.selectedRange = NSMakeRange(0, 0)
我正在开发一个新应用程序,我 运行 遇到了此处描述的相同问题,以防止取消选择输入到 nstextfield 中的文本:http://www.cocoabuilder.com/archive/cocoa/195313-nstextfield-how-to-deselect-text.html
有很多关于使用 NSTextViews 执行此操作的问题,但我无法为 NSTextFields 找到有效的答案。
在我的项目中,我有一个 window,里面有一个文本字段和一个控制器 class,它也是文本字段的委托。我有一个 IBAction 用于在输入时发送的文本字段,它根据字段中的文本执行操作,以及:
(void)controlTextDidChange:(NSNotification *)note
和
(BOOL)control:(NSControl *)control
textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
它处理一些自定义自动完成,如对 Suppressing the text completion dropdown for an NSTextField
的回答我的问题在于,当我按回车键提交输入的文本时,字段中的字符串被完全选中,但我希望能够取消选择文本并将插入点放在末尾,如前所述在第一个 link.
我可以在多个地方取消选择文本,但实际上取消选择不起作用。我已经尝试按照第一个 link 中的描述获取字段编辑器,我也尝试使用 controlTextDidEndEditing: 方法,因为我确实在上面的 controlTextDidChange: 方法中获得了一个字段编辑器:
- (void)controlTextDidEndEditing:(NSNotification *)note{
NSTextView *textView = [[note userInfo] objectForKey:@"NSFieldEditor"];
[textView setSelectedRange:NSMakeRange([[self.ParseField stringValue]length]-1, 0)
affinity:NSSelectionAffinityUpstream
stillSelecting:NO];
}
我也试过在该字段上禁用和重新启用编辑,但这也没有用。
像能够向文本字段发送 moveDown: 消息这样简单的事情对我有用,因为它与按下向下箭头相同,但文本字段无法识别该选择器。 (我以为 NSTextField 继承自 NSResponder,所以它会起作用,但我猜不是?)
感谢您的帮助
在一个只有 window 和文本字段的简单应用程序中。
我将文本字段设置为 firstResponder。
这使得文本在应用 运行s 时被选中。
如果我添加到 applicationDidFinishLaunching
NSRange tRange = [[ _theTextField currentEditor] selectedRange];
[[ _theTextField currentEditor] setSelectedRange:NSMakeRange(tRange.length,0)];
现在 运行 未选中文本且插入点位于文本末尾时。
我使用 selectedRange 来获取选择字符长度。由于文本是一开始就选中的,看起来比较简单。
- (void)awakeFromNib{
[_theTextField setStringValue:@"100000"];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[[[NSApplication sharedApplication] mainWindow] makeFirstResponder:_theTextField];
NSRange tRange = [[ _theTextField currentEditor] selectedRange];
[[ _theTextField currentEditor] setSelectedRange:NSMakeRange(tRange.length,0)];
}
我一直在寻找 Swift 3 中的解决方案,所以这对我有用:
theTextField.currentEditor()?.selectedRange = NSMakeRange(0, 0)