如何以这种方式限制几个UItextField的长度:
最大整数允许两个和最大十进制允许一个。
我已使用此代码将文本限制为两个数字:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var kMaxLength = 2
let oldString = textFieldlOne.text as NSString
let string = oldString.stringByReplacingCharactersInRange(range, withString: string)
return countElements(string) <= kMaxLength
}
但我有几个UITextfield IBOutlet,我希望允许用户插入一个十进制数es。 12.5
答案 0 :(得分:5)
在尝试测试字符串是否与特定模板匹配时,正则表达式非常有用:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let oldString = textField.text ?? "" as NSString
let candidate = oldString.stringByReplacingCharactersInRange(range, withString: string)
let regex = try? NSRegularExpression(pattern: "^\\d{0,2}(\\.\\d?)?$", options: [])
return regex?.firstMatchInString(candidate, options: [], range: NSRange(location: 0, length: candidate.characters.count)) != nil
}
\d{0,2}
匹配零到两位数。 (\.\d?)?
转换为“如果有小数点,则允许它,并且可选地再增加一个数字。” (你也可以(\.\d{0,1})?
。^
匹配“字符串的开头”,$
匹配“字符串的结尾”(即,我们只会找到\d{0,2}(\.\d?)?
是整个字符串的匹配项,而不仅仅是在字符串中间显示的内容。)当然,所有这些\
个字符都被转义为{{1在字符串文字中。
正则表达式(正则表达式)在您第一次遇到它们时可能会有点“密集”,但是一旦您熟悉语法,它就是一个非常强大的工具。
-
顺便说一下,我注意到你在评论中说你正在使用Swift 1.1。我相信等效的Swift 1.1语法类似于:
\\
我没有现成的Swift 1.1编译器,所以我不能轻易地测试/确认这个Swift 1.1语法,但如果我没记错的话,就是这样的。就个人而言,我建议升级到最新版本的Xcode,但每个人都要自己升级。
答案 1 :(得分:1)
这是从@Vijayts回答的快速转换:
struct Constants {
static let MAX_BEFORE_DECIMAL_DIGITS = 2
static let MAX_AFTER_DECIMAL_DIGITS = 1
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let computationString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
// Take number of digits present after the decimal point.
let arrayOfSubStrings = computationString.componentsSeparatedByString(".")
if arrayOfSubStrings.count == 1 && computationString.characters.count > Constants.MAX_BEFORE_DECIMAL_DIGITS {
return false
} else if arrayOfSubStrings.count == 2 {
let stringPostDecimal = arrayOfSubStrings[1]
return stringPostDecimal.characters.count <= Constants.MAX_AFTER_DECIMAL_DIGITS
}
return true
}
答案 2 :(得分:0)
此时我并不擅长迅速。所以我会先尝试在Obj-C中解决它。
#define MAX_BEFORE_DECIMAL_DIGITS = 2
#define MAX_AFTER_DECIMAL_DIGITS = 1
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *computationString = [textField.text stringByReplacingCharactersInRange:range withString:string];
// Take number of digits present after the decimal point.
NSArray *arrayOfSubStrings = [newString componentsSeparatedByString:@"."];
if(([arrayOfSubStrings count] == 1) && (computationString.length > MAX_BEFORE_DECIMAL_DIGITS)) {
return NO;
} else if([arrayOfSubStrings count] == 2) {
NSString *stringPostDecimal = [NSString stringWithFormat:@"%@",[arrayOfSubStrings objectAtIndex:1]];
return !([stringPostDecimal length] > MAX_AFTER_DECIMAL_DIGITS);
}
return YES;
}
根据您的要求定义MAX_BEFORE_DECIMAL_DIGITS和MAX_AFTER_DECIMAL_DIGITS的值。
答案 3 :(得分:0)
Swift 4
使用数字格式化程序
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let s = (textField.text ?? "").appending(string)
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
return numberFormatter.number(from: s)?.doubleValue != nil
}
答案 4 :(得分:0)
在swift 3中,限制文本字段在十进制之前和之后的输入值
let digitBeforeDecimal = 2
let digitAfterDecimal = 2
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let computationString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
let arrayOfSubStrings = computationString.components(separatedBy: ".")
if arrayOfSubStrings.count == 1 && computationString.count > digitBeforeDecimal {//
return false
} else if arrayOfSubStrings.count == 2 {
let stringPostDecimal = arrayOfSubStrings[1]
return stringPostDecimal.count <= digitAfterDecimal
}
return true
}