// OC版本
/// 用于控制金额输入
///
/// - Parameters:
/// - textField:
/// - range:
/// - string:
/// - decimalNumber: 小数点后几位
/// - Returns: bool值,建议直接在 textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Boo 调用
inline static bool isValidAboutInputTextField(UITextField *textfild, NSRange range, NSString * replacementString, NSInteger decimalNumber) {
NSScanner *scanner = [NSScanner scannerWithString:replacementString];
NSCharacterSet *numbers;
NSRange pointRange = [textfild.text rangeOfString:@"."];
if (pointRange.location != NSNotFound && (pointRange.location > range.location + range.length || pointRange.location < range.location)) {
numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
} else {
numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
}
if ([textfild.text isEqualToString:@""] && [replacementString isEqualToString:@"."]) { return false; }
NSString *tempStr = [textfild.text stringByAppendingString:replacementString];
if (pointRange.location != NSNotFound ) {
if ([replacementString isEqualToString:@"."]) { return false; }
if ( tempStr.length > 0 && (tempStr.length - pointRange.location) > decimalNumber + 1) { return false; }
}
NSRange zeroRange = [textfild.text rangeOfString:@"0"];
if (zeroRange.location == 0 && zeroRange.length == 1) {
if(pointRange.length == 0 && pointRange.location > 0){
if([replacementString isEqualToString:@"0"]){
return false;
}
}
}
NSString *buffer;
if (![scanner scanCharactersFromSet:numbers intoString:&buffer] && replacementString.length) {
return false;
} else {
return true;
}
}
// Swift版本
/// 用于控制金额输入
///
/// - Parameters:
/// - textField:
/// - range:
/// - string:
/// - decimalNumber: 小数点后几位
/// - Returns: bool值,建议直接在 textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Boo 调用
func isValidAboutInputTextField(textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String, decimalNumber: Int) -> Bool {
let scanner = Scanner.init(string: string)
var numbers: CharacterSet
let pointRange = textField.text!.toNSRange(textField.text!.range(of: "."))
if ((pointRange?.location)! != NSNotFound && ((pointRange?.location)! > range.location + range.length) || (pointRange?.location)! < range.location) {
numbers = CharacterSet.init(charactersIn: "0123456789")
} else {
numbers = CharacterSet.init(charactersIn: "0123456789.")
}
if textField.text == "" && string == "." { return false }
let tempStr = textField.text! + string
if pointRange?.location != NSNotFound {
if string == "." { return false }
if tempStr.count > 0 && (tempStr.count - (pointRange?.location)! > decimalNumber + 1) { return false }
}
let zeroRange = textField.text!.toNSRange((textField.text?.range(of: "0")))
if zeroRange?.location == NSNotFound && zeroRange?.length == 1 {
if (pointRange?.location)! > 0 && pointRange?.length == 0 {
if string == "0" {
return false
}
}
}
var buffer = ""
if !scanner.scanCharacters(from: numbers, into: AutoreleasingUnsafeMutablePointer(&buffer)) && string.count > 0 {
return false
} else {
return true
}
}
其中swift因为Range 和 NSRange转换及其麻烦,并且swift的Range类型难以驾驭,特提供 String+Extensions进行拓展。和上面的方法搭配更加好用
func toNSRange(_ range: Range<String.Index>?) -> NSRange? {
guard let from = range?.lowerBound.samePosition(in: utf16), let to = range?.upperBound.samePosition(in: utf16) else {
return NSMakeRange(NSNotFound, 0)
}
return NSMakeRange(utf16.distance(from: utf16.startIndex, to: from), utf16.distance(from: from, to: to))
}
iOS 一次性解决关于金额输入问题
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 【蝴蝶效应】 蝴蝶效应:上个世纪70年代,美国一个名叫洛伦兹的气象学家在解释空气系统理论时说,亚马逊雨林一只蝴蝶...