每个人,我都需要根据String的长度逐个更新String / TextField。
例如:如果用户插入43041058,则应更改为4.304.105-8, 否则,如果用户再输入一个数字(共12个字符)430410582,则应更新为43.041.058-2。
实时执行,例如:
Step | User Input
1 | 4.
2 | 4.3
3 | 4.30
4 | 4.304.
5 | 4.304.1
6 | 4.304.10
7 | 4.304.105
8 | 4.304.105-8
9 | 43.041.058-2
重要的是,textField会针对每个按下的角色更新自身,我拥有的壁橱只能在12个位置工作,而不是11个位置。
override func viewDidLoad() {
super.viewDidLoad()
rutFormatter()
}
func rutFormatter() {
textRut.addTarget(self, action: #selector(VCLogin.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
}
@objc func textFieldDidChange(_ textField: UITextField) {
var newRut = String()
for (index, character) in (textField.text?.enumerated())! {
if index % 2 == 0 {
if index != 0 && index % 2 == 0 {
switch index {
case 2:
if character != "." {
newRut.append(".")
}
case 10:
if character != "-" {
newRut.append("-")
}
default:
print("Default")
}
}
if index != 0 && index % 3 == 0 {
switch index {
case 6:
if character != "." {
newRut.append(".")
}
default:
print("Default")
}
}
}
newRut.append(String(character))
}
textField.text = newRut
}
答案 0 :(得分:0)
以下代码应该可以使用。
width: '50%'