简体   繁体   English

如何确定UITextField的光标位置?

[英]How to determine the cursor position of a UITextField?

I have a text field that is always of the format "XX0000000XX", X being a letter, 0 being a number. 我有一个文本字段,该文本字段的格式始终为“ XX0000000XX”,X是字母,0是数字。 I switch keyboards as the user is typing depending on whether they need to enter a number or letter. 当用户键入时,我会根据需要输入数字还是字母来切换键盘。

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? 如何确定UITextField中的光标位置?

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? 我知道一个UITextView有一个selectedRange可以使用,但是我读过你不能强迫UITextView是单行输入吗? ie Disable multiple lines of text. 即禁用多行文本。

Clever bit with the keyboards. 巧妙地使用键盘。 My immediate thought is that perhaps that you shouldn't allow the user to move the cursor in the first place. 我的直接想法是,也许您不应该允许用户首先移动光标。

This isn't without precedent in the iPhone UI. 这在iPhone UI中并非没有先例。 The telephone keypad view, for example, restricts the user to sequential input. 例如,电话键盘视图将用户限制为顺序输入。 I have also found this technique useful for currency input. 我还发现此技术对于货币输入很有用。 Anywhere with input that has a very rigid syntax, basically, seems like a candidate for this kind of treatment. 基本上,任何具有非常严格的语法输入的地方都似乎是这种处理的候选对象。 Might work well for your situation. 可能适合您的情况。

Not sure if this is the best method, but here's how I do it: 不知道这是否是最好的方法,但是这是我的方法:

A UITextField to capture input and a UILabel to display the input. 一个UITextField捕获输入,一个UILabel显示输入。 The text field is hidden but is sent becomeFirstResponder to trigger the keyboard. 文本字段被隐藏,但被发送为FirstResponder以触发键盘。 As the user types, delegate methods do their thing to format the text, if necessary (as with currency), and update the UILabel, which provides the user feedback on their input. 当用户输入内容时,委托方法会执行必要的操作以格式化文本(如有必要,如使用货币),并更新UILabel,该UILabel将为用户提供有关其输入的反馈。

Depending upon the situation, you may wish to style the UILabel in such a way that makes it clear the user can't use it as they would a selectable text field while reassuring them that it is, in fact, their input. 根据情况,您可能希望对UILabel进行样式设置,以使其清楚用户无法使用它,就像他们可以选择的文本字段一样,同时确保他们实际上是他们的输入。

With 11 characters in play, I can see how curser editing might be useful. 在播放11个字符的情况下,我可以看到光标编辑可能会很有用。 Still, phone numbers are often in that same range and I never have a problem with the sequential editor in the Phone app. 尽管如此,电话号码通常仍在同一范围内,而我在“电话”应用程序中的顺序编辑器从来没有问题。

There's a UITextDelegate method -textField:shouldChangeCharactersInRange:replacementString: which should be all you need to do what you describe. 有一个UITextDelegate方法-textField:shouldChangeCharactersInRange:replacementString:这应该是您描述所需要的全部。 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. 您不必在乎光标在哪里,只需在乎所建议的更改范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM