How do I force the cursor to the end of a NSTextField?

前端 未结 4 751
梦毁少年i
梦毁少年i 2021-02-19 14:12

I am NSOpenPanel to select a file or folder from a user\'s machine. But when the user clicks \"open\" the cursor is at the beginning of the path that shows up in the text field

相关标签:
4条回答
  • 2021-02-19 14:33

    I got this working on textfields by doing:

    [[self.inputFileTextField currentEditor] moveToEndOfLine:nil];
    
    0 讨论(0)
  • 2021-02-19 14:33

    Two ideas spring to mind. First, you could use -[NSTextView setSelectedRange:]:

    NSTextView * fieldEditor = [thePanel fieldEditor:NO forObject:theTextField];
    
    NSUInteger text_len = [[fieldEditor string] length];
    
    [fieldEditor setSelectedRange:(NSRange){text_len, 0}];
    

    Or you could use one of the NSResponder action methods on the text field, like moveDown:, moveToEndOfLine:, moveToEndOfParagraph:, &c. Faking a "Page Down" or "Down Arrow" keypress with [theTextField keyDown:...] might also work.

    0 讨论(0)
  • 2021-02-19 14:37

    In Swift, since it's 2015:

    self.textField.moveToEndOfDocument(nil)
    
    0 讨论(0)
  • 2021-02-19 14:47

    Here's my Swift solution:

    self.fileTextField.currentEditor()?.moveToEndOfDocument(nil)
    
    0 讨论(0)
提交回复
热议问题