1
我有一个UITextField用于格式化电话号码。我试图添加一个"+" sign
前缀作为第一个字符,不能删除。格式化&检查工作正常,但有一个前缀似乎并不工作。 “ 标志。我知道这是因为shouldChangeCharactersInRange
在我键入第一个数字之前不会被调用,但为什么makePrefix()
函数不添加前缀?将前缀字符添加到UITextField
所以,我被困在其中“的用户点击了的UITextField,但还没有进入角色还没有”时间..
override func viewDidLoad() {
super.viewDidLoad()
makePrefix()
}
func makePrefix() {
let attributedString = NSMutableAttributedString(string: "+")
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0,1))
phoneTextField.attributedText = attributedString
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
if textField == phoneTextField {
textField.typingAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
return false
}
return true
}
func formattedPhoneNumber(updatedTextString: NSString, textField: UITextField) {
textField.text = "\(updatedTextString as String)"
print(updatedTextString)
}