作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在我的项目中,我需要在文本字段中添加文本时进行设备 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/
我是一名优秀的程序员,十分优秀!