UITextField将光标设置为开始文本位置 [英] UITextField set cursor to start text position

查看:265
本文介绍了UITextField将光标设置为开始文本位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要移动光标以在文本字段上的设置焦点上开始文本位置。有可能吗?

I need to move cursor to start text position on set focus on the textfield. Is it possible tot do?

推荐答案

将视图控制器(或其他一些适当的对象)设置为文本字段的委托并实现 textFieldDidBeginEditing:方法,如下所示:

Set your view controller (or some other appropriate object) as the text field's delegate and implement the textFieldDidBeginEditing: method like this:

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

请注意 setSelectedTextRange: UITextInput 的协议方法( UITextField implements),所以你不会直接在 UITextField 文档。

Note that setSelectedTextRange: is a protocol method of UITextInput (which UITextField implements), so you won't find it directly in the UITextField documentation.

这篇关于UITextField将光标设置为开始文本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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