如何在设置其值后在UITextField中移动光标 [英] how to move cursor in UITextField after setting its value

查看:158
本文介绍了如何在设置其值后在UITextField中移动光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我这个:我需要为输入数字实现 UITextField 。此数字应始终为十进制格式,包含4个位置,例如12.3456或12.3400。
所以我创建了 NSNumberFormatter ,这可以帮助我获得小数位。

can anybody help me with this: i need to implement UITextField for input number. This number should always be in decimal format with 4 places e.g. 12.3456 or 12.3400. So I created NSNumberFormatter that helps me with decimal places.

我设置 UITextField

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange) range replacementString:(NSString *)string
方法。
我处理输入,使用格式化程序,最后调用 [textField setText:myFormattedValue];

这是有效的很好,但这个调用也将光标移动到我的字段的末尾。这是不受欢迎的。例如。我的字段中有12.3400,光标位于最开头,用户类型编号为1.结果值为112.3400,但光标在末尾移动。我希望在用户期望时(在最近添加的数字1之后)以光标结束。有一些关于在 TextView 中设置光标的主题,但这是 UITextField 。我还尝试捕获字段的 selectedTextRange ,这样可以正确保存光标位置但是在 setText 方法调用之后,这会自动更改和原点 UITextRange 丢失(更改为当前)。希望我的解释清楚。

This works fine but this call also moves the cursor to the end of my field. That is unwanted. E.g. I have 12.3400 in my field and the cursor is located on the very beginning and user types number 1. The result value is 112.3400 but cursor is moved at the end. I want to end with cursor when the user expects (just after the number 1 recently added). There are some topics about setting cursor in TextView but this is UITextField. I also tried to catch selectedTextRange of the field, which saves the cursor position properly but after setText method call, this automatically changes and the origin UITextRange is lost (changed to current). hopefully my explanation is clear.

请帮助我。非常感谢你。

Please, help me with this. thank you very much.

编辑:最后,我决定在整个编辑后将功能切换为更改格式,并且工作得很好。我通过添加选择器 forControlEvents来完成它:UIControlEventEditingDidEnd

EDIT : Finally, i decided to switch the functionality to changing the format after whole editing and works good enough. I have done it by adding a selector forControlEvents:UIControlEventEditingDidEnd.

推荐答案

更改 UITextField.text 属性后,之前对 UITextPosition UITextRange的任何引用与旧文本关联的对象将设置为nil。在设置文本属性之前,您需要存储操作之后的文本偏移量。

After the UITextField.text property is changed, any previous references to UITextPosition or UITextRange objects that were associated with the old text will be set to nil after you set the text property. You need to store what the text offset will be after the manipulation will be BEFORE you set the text property.

这对我有用(注意,你必须测试是否如果你从下面的例子中的t中删除任何字符,则cursorOffset为< textField.text.length:

This worked for me (note, you do have to test whether cursorOffset is < textField.text.length if you remove any characters from t in the example below):

- (BOOL) textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange) range replacementString:(NSString *) string

{
    UITextPosition *beginning = textField.beginningOfDocument;
    UITextPosition *start = [textField positionFromPosition:beginning offset:range.location];
    UITextPosition *end = [textField positionFromPosition:start offset:range.length];
    UITextRange *textRange = [textField textRangeFromPosition:start toPosition:end];

    // this will be the new cursor location after insert/paste/typing
    NSInteger cursorOffset = [textField offsetFromPosition:beginning toPosition:start] + string.length; 

    // now apply the text changes that were typed or pasted in to the text field
    [textField replaceRange:textRange withText:string];

    // now go modify the text in interesting ways doing our post processing of what was typed...
    NSMutableString *t = [textField.text mutableCopy];
    t = [t upperCaseString];
    // ... etc

    // now update the text field and reposition the cursor afterwards
    textField.text = t;
    UITextPosition *newCursorPosition = [textField positionFromPosition:textField.beginningOfDocument offset:cursorOffset];
    UITextRange *newSelectedRange = [textField textRangeFromPosition:newCursorPosition toPosition:newCursorPosition];
    [textField setSelectedTextRange:newSelectedRange];

    return NO;
}

这篇关于如何在设置其值后在UITextField中移动光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆