将格式化程序附加到 iPhone 上的 UITextField
我正在尝试在用户输入文本时格式化 UITextField,例如显示数千个分隔符。
我找到了这个网页: http:// www.iphonedevsdk.com/forum/iphone-sdk-development/16512-trying-add-commas.html
看来shouldChangeCharactersInRange:不是一个好的解决方案。我想到了一个自定义 UIView,其中视图将随着用户输入数字而更新,但它需要重新创建轮子......
I'm trying to format an UITextField as user types text, for instance to show separator for thousands.
I found this web page : http://www.iphonedevsdk.com/forum/iphone-sdk-development/16512-trying-add-commas.html
It seems that shouldChangeCharactersInRange: is not a good solution. I thought of a custom UIView where the view would be updated as the user types digits, yet it would require recreating the wheel...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 UITextView 有一个回调,每次其内容发生更改时都会触发,然后您可以通过格式化程序运行整个内容。
编辑:对,找到文档 这里 UITextViewTextDidChangeNotification
应该很容易实现,但是,请注意,当用户输入合理数量的文本时,每次击键都会调用您的代码段,我建议在之前检查的文本和新文本之间进行某种差异,否则您将在每次击键时检查一大块文本(例如,您可以添加时间延迟吗?)
I believe UITextView has a callback that fires everytime there is a change to its contents, then you can run the whole contents through a formatter.
EDIT: Right, found the Docs Here The UITextViewTextDidChangeNotification
Should be easy enough to implement, however, a heads up, when the user types in a reasonable amount of text every keystroke will call your piece of code, i suggest making some sort of a diff between text previously checked and new text, otherwise you'll be checking a huge block of text on every keystroke (You could add a time delay for example?)