49

我有一个UITextField我通过修改更改通知处理程序中的文本来强制格式化。这很好用(一旦我解决了重入问题),但给我留下了一个更烦人的问题。如果用户将光标移动到字符串末尾以外的位置,那么我的格式更改会将其移动到字符串的末尾。这意味着用户一次不能在文本字段的中间插入一个以上的字符。有没有办法记住然后重置光标位置UITextField

4

7 回答 7

68

控制 UITextField 中的光标位置很复杂,因为输入框和计算位置涉及很多抽象。但是,这当然是可能的。您可以使用成员函数setSelectedTextRange

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

这是一个函数,它接受一个范围并选择该范围内的文本。如果您只想将光标放在某个索引处,只需使用长度为 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]];
}

例如,将光标放在idxUITextField 中input

    [Helpers selectTextForInput:input 
                        atRange:NSMakeRange(idx, 0)];
于 2012-07-18T00:26:10.000 回答
4

用于定位索引(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)
}
于 2016-11-10T12:48:13.220 回答
3

我终于找到了解决这个问题的方法!您可以将需要插入的文本放入系统粘贴板中,然后将其粘贴到当前光标位置:

[myTextField paste:self]  

我在这个人的博客上找到了解决方案:
http://dev.ragfield.com/2009/09/insert-text-at-current-cursor-location.html

粘贴功能是特定于 OS V3.0 的,但我已经对其进行了测试,它使用自定义键盘对我来说效果很好。

如果您选择此解决方案,那么您可能应该保存用户现有的剪贴板内容并在之后立即恢复它们。

于 2009-10-24T18:32:07.300 回答
2

这是@Chris R. 的 Swift 版本 –为 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)
}
于 2016-10-24T13:08:02.357 回答
1

随意使用此UITextField类别来获取和设置光标位置:

@interface UITextField (CursorPosition)

@property (nonatomic) NSInteger cursorPosition;

@end

——</p>

@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
于 2016-10-26T21:56:17.043 回答
0

我认为没有办法将光标放在您的特定位置UITextField(除非您非常棘手并模拟了触摸事件)。相反,我会在用户完成编辑他们的文本(in textFieldShouldEndEditing:)时处理格式,如果他们的输入不正确,则不允许文本字段完成编辑。

于 2009-09-30T19:43:34.777 回答
-2

这是一个可以很好地解决这个问题的片段:

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

来自@omz

于 2015-05-11T09:05:32.003 回答