如何确定UITextField的光标位置? [英] How to determine the cursor position of a UITextField?

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

问题描述

我有一个文本字段,该文本字段的格式始终为"XX0000000XX",X是字母,0是数字.当用户键入键盘时,我会根据需要输入数字还是字母来切换键盘.

I have a text field that is always of the format "XX0000000XX", X being a letter, 0 being a number. I switch keyboards as the user is typing depending on whether they need to enter a number or letter.

这很好,直到用户将光标置于文本字段的中间以进行编辑.如何确定UITextField中的光标位置?

This works fine, until the user positions the cursor in the middle of the text field to make an edit. How do I determine the cursor position within a UITextField?

我知道一个UITextView有一个selectedRange可以使用,但是我读过你不能强迫UITextView是单行输入吗?即禁用多行文本.

I know a UITextView has a selectedRange that can be used, but I've read that you can't force a UITextView to be single line entry? i.e. Disable multiple lines of text.

推荐答案

有一个UITextDelegate方法-textField:shouldChangeCharactersInRange:replacementString:,它应该是您要做的所有描述.实施它以实施所需的任何格式设置规则.您不必关心光标在哪里,只需关心所建议的更改范围即可.

There's a UITextDelegate method -textField:shouldChangeCharactersInRange:replacementString: which should be all you need to do what you describe. Implement it to enforce whatever formatting rules you want. You shouldn't care where the cursor is, just the range of the change that's proposed.

这篇关于如何确定UITextField的光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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