functextField(_textField:UITextField, shouldChangeCharactersIn range:NSRange, replacementString string:String) ->Bool{
iftextField ==currencyTf{
if currency=="NEO"{
//限制只能输入数字,不能输入特殊字符
letlength = string.lengthOfBytes(using:String.Encoding.utf8)
letfutureString:NSMutableString=NSMutableString(string: textField.text!)
futureString.insert(string, at: range.location)
iffutureString.length>=1{
//the first character can't be '.'
letchar =Character(UnicodeScalar(futureString.character(at:0))!)
ifchar =="0"{
returnfalse;
}
}
forloopIndexin0..
letchar = (stringasNSString).character(at: loopIndex)
ifchar <48{returnfalse}
ifchar >57{returnfalse}
}
}
letfutureString:NSMutableString=NSMutableString(string: textField.text!)
futureString.insert(string, at: range.location)
vardotNum =0;
iffutureString.length>=1{
//the first character can't be '.'
letchar =Character(UnicodeScalar(futureString.character(at:0))!)
ifchar =="."{
returnfalse;
}
//if the first character is '0',the next one must be '.'
iffutureString.length>=2{
letchar2 =Character(UnicodeScalar(futureString.character(at:1))!);
ifchar2 !="."&& char =="0"{
returnfalse;
}
}
}
if!futureString.isEqual(to:"") {
foriin(0..
letchar =Character(UnicodeScalar(futureString.character(at: i))!)
ifchar =="."{
dotNum = dotNum+1;
ifdotNum>1{
returnfalse;
}
}
}
}
}
returntrue;
}