UITextField 显示“ € ”象征
我有一个 UITextField 只允许用户输入数字,即金额。当用户输入 10000 时,文本字段将显示 € 10.000。当用户在文本字段中键入或退格时,它会动态变化。我如何在 iPhone 中实现它?
I have a UITextField only allow users to enter the number, which is amount of money. when user types 10000, the textfield will show € 10.000. And it will change dynamically when user types or backspace in the textfield. How do I implement it in iPhone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要为 UITextField 提供一个委托,并在该委托中为
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
实现一些内容,以检查字段的格式是否为有效并更改文本(如果是),在选定位置插入字符,插入/删除 . / 货币符号,则返回 NO。
棘手的部分是,每当您替换字段的文本时,插入点都会跳转到字段的末尾 - 有一些未记录的 API 可以检索/设置其位置,但不是任何官方 API。在大多数情况下这不是问题,因为用户不太可能在这么小的字段中移动插入点,但您可能会收到一些抱怨。
You'd want to give the UITextField a delegate and implement something for
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
in that delegate that checked to see if the field's format was valid and altered the text if so, inserting the characters in the selected location, inserting / removing the . / currency symbol, then returning NO.
The tricky part is that the insertion point jumps to the end of the field whenever you replace the field's text - there are some undocumented APIs to retrieve / set its position, but not any official ones. Not a problem in most cases, since a user isn't very likely to move the insertion point in a field this small, but you might get a few complaints.