UITextField shouldChangeCharactersInRange 没有检测到第一个字符
UITextField shouldChangeCharactersInRange doesn't detect first character
我有一个 UITextField,我正在尝试将其格式化为 phone 数字,但是由于某种原因,我正在努力获取 UITextField 的第一个字母。
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 来格式化 phone 数字(并使用 PartialFormatter().formatPartial
)
编辑: 我尝试在 textField 文本的开头添加默认的 +
符号(如数字 +44..)
var updatedTextString : NSString = PartialFormatter().formatPartial("+\(textField.text!)")
但是完全错了。它一开始一直打印++++,然后自己修复了4个字符甚至更多。
所以它就像...
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()
分组
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() 函数正在询问是否应应用更改,但它尚未允许修改您的文本字段。
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,我正在尝试将其格式化为 phone 数字,但是由于某种原因,我正在努力获取 UITextField 的第一个字母。
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 来格式化 phone 数字(并使用 PartialFormatter().formatPartial
)
编辑: 我尝试在 textField 文本的开头添加默认的 +
符号(如数字 +44..)
var updatedTextString : NSString = PartialFormatter().formatPartial("+\(textField.text!)")
但是完全错了。它一开始一直打印++++,然后自己修复了4个字符甚至更多。
所以它就像...
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()
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() 函数正在询问是否应应用更改,但它尚未允许修改您的文本字段。
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
}