问题描述
我有一个 UITextField,我正在尝试将其格式化为电话号码,但是由于某种原因,我正在努力获取 UITextField 的第一个字母.
I have a UITextField and I am trying to format it as phone number, however I am struggling getting the first letter of the UITextField for some reason.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
if textField == myTextField {
var updatedTextString : NSString = PartialFormatter().formatPartial(textField.text!)
self.formattedPhoneNumber(updatedTextString, textField: textField)
}
return true
}
func formattedPhoneNumber(updatedTextString: NSString, textField: UITextField) {
textField.text = updatedTextString as String
print(updatedTextString)
}
我做错了什么?
请注意,我使用 PhoneNumberKit 来格式化电话号码(并使用 PartialFormatter().formatPartial)
Please note that I am using PhoneNumberKit in order to format phone number (and use PartialFormatter().formatPartial
)
我尝试在 textField 文本的开头添加一个默认的 +
符号(如数字 +44..)
I tried adding a default +
sign in the beginning of textField's text (as the numbers +44..)
var updatedTextString : NSString = PartialFormatter().formatPartial("+\(textField.text!)")
但它完全出错了.一开始它一直打印++++,然后自己修复了4个字符甚至更多.
But it went completely wrong. It kept printing ++++ at first and then fixes itself for 4 characters and more.
所以它就像......
So it goes like...
I press 1 - `+1` on UITextField - `+` on console
then press 5 - `++15` on UITextField - `++1` on console
then press 6 - `+++156` on UITextField - `+++15` on console
then press 8 - `+1568` on UITextField - `+1 56` on console
// so it magically fixed itself after 4 chars.
Also removing everything leaves `+++` on UITextField and `++++` on console
and doesn't delete more
我做错了什么?
编辑 2:
我尝试使用下面发布的答案.然而现在,它只是简单的数字.它们不与 PartialFormatter().formatPartial()
I tried using the answer posted below. However now, it's just plain numbers. They are not grouped with PartialFormatter().formatPartial()
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
if textField == myTextField {
let textString : NSString = textField.text!
let candidateString : NSString = textString.stringByReplacingCharactersInRange(range, withString: string)
let updatedTextString : NSString = PartialFormatter().formatPartial(candidateString as String)
self.formattedPhoneNumber(updatedTextString, textField: textField)
}
return true
}
推荐答案
shouldChangeCharactersInRange() 函数询问是否应应用更改,但尚未允许修改您的文本字段.
The shouldChangeCharactersInRange() function is asking whether the change should be applied but it has not yet allowed your textfield to be modified.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
if textField == myTextField {
let textString : NSString = textField.text!
let candidateString : NSString = textString.stringByReplacingCharactersInRange(range, withString: string)
let updatedTextString : NSString = PartialFormatter().formatPartial(candidateString as String)
print(updatedTextString)
textField.text = updatedTextString
}
return true
}
这篇关于UITextField shouldChangeCharactersInRange 不检测第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!