0

I have a UITextField, how can I change the cursor position dynamically based of some condition to a particular length?

4

2 回答 2

1

使用textview.selectedRange.

NSRange selection = [textview.text rangeOfString:@"SomeText" options:NSCaseInsensitiveSearch];
if( selection.location != NSNotFound ){
    textview.selectedRange =  selection;
    textview.selectedRange = NSMakeRange(selection.location, 0);
}

它可能会帮助你

于 2012-10-10T11:28:10.847 回答
0

使用此链接可能会帮助您解决问题。希望有用!!:)

设置新文本时更改 UITextField 中的光标

以及在 UITextField 中查找光标位置

// Get the selected text range
    UITextRange *selectedRange = [self selectedTextRange];

    // Calculate the existing position, relative to the end of the field (will be a - number)
    int pos = [self offsetFromPosition:self.endOfDocument toPosition:selectedRange.start];

    // Change the text
    self.text = lastValidated; 

    // Work out the position based by offsetting the end of the field to the same
    // offset we had before editing
    UITextPosition *newPos = [self positionFromPosition:self.endOfDocument offset:pos];

    // Reselect the range, to move the cursor to that position
    self.selectedTextRange = [self textRangeFromPosition:newPos toPosition:newPos];

快乐的编码......

于 2012-10-10T11:24:40.917 回答