functextField(_textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) ->Bool{
ifstring.isNonEmpty(),varstr = textField.text{
str.insert(Character.init(string), at: str.index(str.startIndex, offsetBy: range.location))
letlocation = (range.location==3|| range.location==8) ? (range.location+2) : (range.location+1)
textFieldEditing(textField, str: str, location: location)
returnfalse
}else{
ifvarstr = textField.text{
ifrange.location==3|| range.location==8{
letstart = textField.position(from: textField.beginningOfDocument, offset: range.location)
letend = textField.position(from: textField.beginningOfDocument, offset: range.location)
textField.selectedTextRange= textField.textRange(from: start!, to: end!)
returnfalse
}else{
str.remove(at: str.index(str.startIndex, offsetBy: range.location))
letlocation = (range.location==3|| range.location==8|| range.location==4|| range.location==9) ? (range.location-1) : (range.location)
textFieldEditing(textField, str: str, location: location)
returnfalse
}
}
}
return true
}
functextFieldEditing(_textField:UITextField, str:String, location:Int) {
varstr = str.replacingOccurrences(of:" ", with:"")
ifstr.count>3{
str.insert(" ", at: str.index(str.startIndex, offsetBy:3))
}
ifstr.count>8{
str.insert(" ", at: str.index(str.startIndex, offsetBy:8))
}
textField.text= str
GMLoginCodeView.afterPhoneText = str
letstart = textField.position(from: textField.beginningOfDocument, offset: location)
letend = textField.position(from: textField.beginningOfDocument, offset: location)
textField.selectedTextRange= textField.textRange(from: start!, to: end!)
}
以下支持粘贴:
// String:新输入或者粘贴过来的文字
// range:输入的范围
functextField(_textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) ->Bool{
ifstring.isNonEmpty(),varstr = textField.text{
ifstr.isNonEmpty() {
varlocation =0
ifstring.count==1{
str.insert(Character.init(string), at: str.index(str.startIndex, offsetBy: range.location))
location = (range.location==3|| range.location==8) ? (range.location+2) : (range.location+1)
textFieldEditing(textField, str: str, location: location)
}else{
str += string
textFieldEditing(textField, str: str, location:15)
}
}else{
str += string
textFieldEditing(textField, str: str, location:15)
}
returnfalse
}else{
ifvarstr = textField.text{
ifrange.location==3|| range.location==8{
letstart = textField.position(from: textField.beginningOfDocument, offset: range.location)
letend = textField.position(from: textField.beginningOfDocument, offset: range.location)
textField.selectedTextRange= textField.textRange(from: start!, to: end!)
returnfalse
}else{
str.remove(at: str.index(str.startIndex, offsetBy: range.location))
letlocation = (range.location==3|| range.location==8|| range.location==4|| range.location==9) ? (range.location-1) : (range.location)
textFieldEditing(textField, str: str, location: location)
returnfalse
}
}
}
return true
}
functextFieldEditing(_textField:UITextField, str:String, location:Int) {
varstr = str.replacingOccurrences(of:" ", with:"")
ifstr.count>3{
str.insert(" ", at: str.index(str.startIndex, offsetBy:3))
}
ifstr.count>8{
str.insert(" ", at: str.index(str.startIndex, offsetBy:8))
}
textField.text= str
GMLoginCodeView.afterPhoneText = str
iflocation !=15{
//设置光标位置
letstart = textField.position(from: textField.beginningOfDocument, offset: location)
letend = textField.position(from: textField.beginningOfDocument, offset: location)
textField.selectedTextRange= textField.textRange(from: start!, to: end!)
}
}
网友评论