Control cursor position in UITextField

后端 未结 7 2149
你的背包
你的背包 2020-11-29 00:52

I have a UITextField that I\'m forcing formatting on by modifying the text inside the change notification handler. This works great (once I solved the reentranc

相关标签:
7条回答
  • 2020-11-29 01:08

    Here's the Swift version of @Chris R. – updated for Swift3

    private func selectTextForInput(input: UITextField, range: NSRange) {
        let start: UITextPosition = input.position(from: input.beginningOfDocument, offset: range.location)!
        let end: UITextPosition = input.position(from: start, offset: range.length)!
        input.selectedTextRange = input.textRange(from: start, to: end)
    }
    
    0 讨论(0)
  • 2020-11-29 01:12

    Controlling cursor position in a UITextField is complicated because so many abstractions are involved with input boxes and calculating positions. However, it's certainly possible. You can use the member function setSelectedTextRange:

    [input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
    

    Here's a function which takes a range and selects the texts in that range. If you just want to place the cursor at a certain index, just use a range with length 0:

    + (void)selectTextForInput:(UITextField *)input atRange:(NSRange)range {
        UITextPosition *start = [input positionFromPosition:[input beginningOfDocument] 
                                                     offset:range.location];
        UITextPosition *end = [input positionFromPosition:start
                                                   offset:range.length];
        [input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
    }
    

    For example, to place the cursor at idx in the UITextField input:

        [Helpers selectTextForInput:input 
                            atRange:NSMakeRange(idx, 0)];
    
    0 讨论(0)
  • 2020-11-29 01:23

    Useful to position at index (Swift 3)

    private func setCursorPosition(input: UITextField, position: Int) {
        let position = input.position(from: input.beginningOfDocument, offset: position)!
        input.selectedTextRange = input.textRange(from: position, to: position)
    }
    
    0 讨论(0)
  • 2020-11-29 01:29

    Here is a snippet which works fine for this problem:

    - (void)textFieldDidBeginEditing:(UITextField *)textField{
        UITextPosition *positionBeginning = [textField beginningOfDocument];
        UITextRange *textRange =[textField textRangeFromPosition:positionBeginning 
                                                      toPosition:positionBeginning];
        [textField setSelectedTextRange:textRange];
    }
    

    Source from @omz

    0 讨论(0)
  • 2020-11-29 01:30

    Feel free to use this UITextField category to get and set cursor position:

    @interface UITextField (CursorPosition)
    
    @property (nonatomic) NSInteger cursorPosition;
    
    @end
    

    @implementation UITextField (CursorPosition)
    
    - (NSInteger)cursorPosition
    {
        UITextRange *selectedRange = self.selectedTextRange;
        UITextPosition *textPosition = selectedRange.start;
        return [self offsetFromPosition:self.beginningOfDocument toPosition:textPosition];
    }
    
    - (void)setCursorPosition:(NSInteger)position
    {
        UITextPosition *textPosition = [self positionFromPosition:self.beginningOfDocument offset:position];
        [self setSelectedTextRange:[self textRangeFromPosition:textPosition toPosition:textPosition]];
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-29 01:31

    I've finally found a solution for this problem! You can put the text you need inserted into the system pasteboard and then paste it at the current cursor position:

    [myTextField paste:self]  
    

    I found the solution on this person's blog:
    http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html

    The paste functionality is OS V3.0 specific, but I've tested it and it works fine for me with a custom keyboard.

    If you go for this solution then you should probably save the user's existing clipboard contents and restore them immediately afterwards.

    0 讨论(0)
提交回复
热议问题