如何在 ios 中结合密码点和普通文本制作 UITextField
How to make UITextField with combination of password-dot and normal text in ios
目前我的文本字段在 UITextField 中将输入作为“111-11-1111”。要求输入“•••-••-1111”。这是否可以通过安全文本输入和普通文本的组合或任何其他方式来完成。
您必须使用
检查文本字段中的每个文本条目
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
这个方法。在输入特定次数后,您在密码中附加了 -。
你可以这样实现。
将变量声明为 textCount
var textCount = 0
var originalString = String()
然后重写函数 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 如下:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string == "" {
textCount = textCount - 1
var truncated = textField.text?.substring(to: (textField.text?.index(before: (textField.text?.endIndex)!))!)
textField.text = truncated
if textCount == 5 || textCount == 3 {
let truncated = textField.text?.substring(to: (textField.text?.index(before: (textField.text?.endIndex)!))!)
textField.text = truncated
}
truncated = originalString.substring(to: originalString.index(before: originalString.endIndex))
originalString = truncated!
}
else if textCount < 9 {
if textCount == 3 || textCount == 5 {
textField.text = textField.text! + "-"
}
if textCount <= 4 {
textField.text = textField.text! + "*"
originalString.append(string)
}
else {
textField.text = textField.text! + string
originalString.append(string)
}
textCount = textCount + 1
}
print(originalString)
return false
}
请记住将文本字段的文本存储在委托本身中。否则它会给你错误的价值。
目前我的文本字段在 UITextField 中将输入作为“111-11-1111”。要求输入“•••-••-1111”。这是否可以通过安全文本输入和普通文本的组合或任何其他方式来完成。
您必须使用
检查文本字段中的每个文本条目- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
这个方法。在输入特定次数后,您在密码中附加了 -。
你可以这样实现。
将变量声明为 textCount
var textCount = 0
var originalString = String()
然后重写函数 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 如下:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string == "" {
textCount = textCount - 1
var truncated = textField.text?.substring(to: (textField.text?.index(before: (textField.text?.endIndex)!))!)
textField.text = truncated
if textCount == 5 || textCount == 3 {
let truncated = textField.text?.substring(to: (textField.text?.index(before: (textField.text?.endIndex)!))!)
textField.text = truncated
}
truncated = originalString.substring(to: originalString.index(before: originalString.endIndex))
originalString = truncated!
}
else if textCount < 9 {
if textCount == 3 || textCount == 5 {
textField.text = textField.text! + "-"
}
if textCount <= 4 {
textField.text = textField.text! + "*"
originalString.append(string)
}
else {
textField.text = textField.text! + string
originalString.append(string)
}
textCount = textCount + 1
}
print(originalString)
return false
}
请记住将文本字段的文本存储在委托本身中。否则它会给你错误的价值。