将光标移动到 UITextField 的开头
有没有办法让光标位于 UITextField 的开头?
当我显示带有内容的控件时,光标放置在文本的末尾。我想把它移到开头。
Is there a way to make the cursor be at the start of a UITextField
?
When I display the control with content, the cursor is placed at the end of the text. I'd like to move it to the beginning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UITextField
符合UITextInput
协议,它提供了让您控制所选范围的方法。这在我的测试中有效:UITextField
conforms to theUITextInput
protocol, which provides methods that let you control the selected range. This works in my testing:你正在与这个系统作斗争。
UITextField
没有任何公共属性来设置光标位置(实际上与当前选择的开头相关)。如果您可以使用UITextView
代替,则以下委托方法将强制光标位于文本的开头。请注意,用户不会期望这种行为,您应该仔细检查您想要这样做的动机。其中
shouldMoveCursor
是您在控制器中维护的BOOL
变量。You're fighting the system on this one.
UITextField
does not have any public properties to set the cursor position (which actually correlates to the beginning of the current selection). If you can use aUITextView
instead, the following delegate methods will force the cursor to the beginning of the text. Just be aware that users won't expect this behavior and you should double-check your motives for wanting to do it.Where
shouldMoveCursor
is aBOOL
variable you maintain in your controller.为我工作
works for me