我正在制作一个iOS
Xcode 7.3 Swift2
项目。它具有不同的UITextFields
,仅限于 3 位,特别是数字。它们被分配到UITextFieldDelegate
并且运行良好。
这是我限制它们的地方:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
let limitLength = 3
if newLength > limitLength {
return false
}
let numberOnly = NSCharacterSet.init(charactersInString: "0123456789")
let stringFromTextField = NSCharacterSet.init(charactersInString: string)
let strValid = numberOnly.isSupersetOfSet(stringFromTextField)
return strValid
}
但是,UITextFields
中的某些UITextFields
需要仅限于数字且仅限于单个数字,我如何才能在上面的部分中对此进行限制,仅适用于那些特定的{ {1}}?
需要为单个数字的UITextFields
的名称为:
widthInches
lengthInches
我尝试将这个放在第一个后卫部分之后没有运气:
guard let text2 = widthInches.text else { return true }
let newLength2 = text2.characters.count + string.characters.count - range.length
let limitLength2 = 3
if newLength2 > limitLength2 {
return false
}
答案 0 :(得分:5)
实际上我在这里使用textfield标签。因为自定义文本字段。 如果您在此条件标记中使用TextfieldEffect等自定义文本字段,则可以帮助您限制Textfield。
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{
if textField.tag == txtCountryCode.tag{
let maxLength = 4
let currentString: NSString = textField.text!
let newString: NSString =
currentString.stringByReplacingCharactersInRange(range, withString: string)
return newString.length <= maxLength
}
if textField.tag == txtMobileNumber.tag{
let maxLength = 10
let currentString: NSString = textField.text!
let newString: NSString =
currentString.stringByReplacingCharactersInRange(range, withString: string)
return newString.length <= maxLength
}
return true
}
我希望这会对你有所帮助。
答案 1 :(得分:2)
函数shouldChangeCharactersInRange
将特定textField作为其参数之一传递。您可以查看它,看看它是否指向与您想要缩短的实例相同的实例,如下所示:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return true }
var limitLength = 3
if textField == widthInches || textField == lengthInches {
limitLength = 1
}
let newLength = text.characters.count + string.characters.count - range.length
if newLength > limitLength {
return false
}
let numberOnly = NSCharacterSet.init(charactersInString: "0123456789")
let stringFromTextField = NSCharacterSet.init(charactersInString: string)
let strValid = numberOnly.isSupersetOfSet(stringFromTextField)
return strValid
}
假设所有其他要求相同(仅限数字),这将起到作用。
还有其他方法,例如 - 你可以继承UITextField并添加一个limitLength字段,然后在委托中使用该字段,但这可能仅仅是2个例外。
答案 2 :(得分:1)
Hello func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
textField
param是触发此事件的textField,因此您可以检查文本字段对象,如果等于其中一个,则进行不同的行为
我希望这可以帮到你,
答案 3 :(得分:1)
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
return (textField.text?.utf16.count ?? 0) + string.utf16.count - range.length <= TEXT_FIELD_LIMIT
}
这计算基于UTF-16表示的字符数,因为range.length以UTF-16为基础给出。如果需要以其他方式计算字符数,表达式可能会更长。如果您只想输入数字,请使用textField.keyboardType = UIKeyboardTypeDecimalPad
。如果你想要特定的textFields然后添加标签并进行比较,如果它们相等,你就可以为它实现特定的代码。
答案 4 :(得分:0)
更新swift 3添加此类并将其命名为TextField.swift。它将在故事板上添加限制输入。
import UIKit
private var maxLengths = [UITextField: Int]()
extension UITextField {
@IBInspectable var maxLength: Int {
get {
guard let length = maxLengths[self] else {
return Int.max
}
return length
}
set {
maxLengths[self] = newValue
// Any text field with a set max length will call the limitLength
// method any time it's edited (i.e. when the user adds, removes,
// cuts, or pastes characters to/from the text field).
addTarget(
self,
action: #selector(limitLength),
for: UIControlEvents.editingChanged
)
}
}
func limitLength(textField: UITextField) {
guard let prospectiveText = textField.text,
prospectiveText.characters.count > maxLength else {
return
}
// If the change in the text field's contents will exceed its maximum
length,
// allow only the first [maxLength] characters of the resulting text.
let selection = selectedTextRange
// text = prospectiveText.substring(with:Range<String.Index>
(prospectiveText.startIndex ..< prospectiveText.index(after: maxLength))
let s = prospectiveText
// Get range 4 places from the start, and 6 from the end.
let c = s.characters;
let r = c.index(c.startIndex, offsetBy: 0)..<c.index(c.endIndex, offsetBy: maxLength - c.count)
text = s[r]
// Access the string by the range.
selectedTextRange = selection
}
}
或在此处下载 - &gt; TextField.swift