gpt4 book ai didi

ios - 如何在 Swift 4 中更改文本字段中的文本时每 2 个字符后添加分隔符

转载 作者:搜寻专家 更新时间:2023-11-01 06:00:37 24 4
gpt4 key购买 nike

在我的项目中,我需要在文本字段中添加文本时进行设备 MAC 地址验证。我的设备 mac 地址格式是“AB:5E:CF:45:S5:6D”。因此,无论何时我要添加任何字符,都应该通过在文本字段中的 2 个字符后添加冒号来更改。

示例:“45DF5R6” = “45:DF:5R:6” “SD45ER65DF6G” = “SD:45:ER:65:DF:6G”

感谢您的帮助。

提前致谢。

最佳答案

试试这个

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text = (textField.text! as NSString).replacingCharacters(in: range, with: string)
let count = text.count
if string != "" {
if count > 17
{
return false
}
if count % 3 == 0{
txtField.text?.insert(":", at: String.Index.init(encodedOffset: count - 1))
}
return true
}
return true
}

关于ios - 如何在 Swift 4 中更改文本字段中的文本时每 2 个字符后添加分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52479082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com