Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UITextField中的最大字符数以及仅限制数值_Swift_Uitextfield - Fatal编程技术网

Swift UITextField中的最大字符数以及仅限制数值

Swift UITextField中的最大字符数以及仅限制数值,swift,uitextfield,Swift,Uitextfield,是否有一种方法可以将UITextField限制为仅限数值以及限制长度 我有以下两个函数,但不知道如何在同一个委托中两次使用shouldChangeCharacters。你知道如何使用这两种方法吗 // Allow Numeric Only in Quantity func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: Strin

是否有一种方法可以将UITextField限制为仅限数值以及限制长度

我有以下两个函数,但不知道如何在同一个委托中两次使用shouldChangeCharacters。你知道如何使用这两种方法吗

 // Allow Numeric Only in Quantity
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,     replacementString string: String) -> Bool {
    let allowCharacters = ".-+1234567890"
    let allowedCharacterSet = CharacterSet(charactersIn: allowCharacters)
    let typedCharacterSet = CharacterSet(charactersIn: string)

    return allowedCharacterSet.isSuperset(of: typedCharacterSet)
}



// Limit the length of the input
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard let textFieldText = textField.text,
        let rangeOfTextToReplace = Range(range, in: textFieldText) else {
            return false
    }

    let substringToReplace = textFieldText[rangeOfTextToReplace]
    let count = textFieldText.count - substringToReplace.count + string.count
    return count <= 20
}
//仅允许数量为数字
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
让allowCharacters=“.-+1234567890”
让allowedCharacterSet=CharacterSet(charactersIn:allowCharacters)
让typedCharacterSet=CharacterSet(charactersIn:string)
返回allowedCharacterSet.isSuperset(of:typedCharacterSet)
}
//限制输入的长度
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
guard let textFieldText=textField.text,
让rangeOfTextToReplace=Range(Range,in:textFieldText)else{
返回错误
}
let substringToReplace=textFieldText[rangeOfTextToReplace]
let count=textFieldText.count-substringstorereplace.count+string.count

返回计数您可以这样做

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

   guard let textFieldText = textField.text,
       let rangeOfTextToReplace = Range(range, in: textFieldText) else {
               return false
       }

   let substringToReplace = textFieldText[rangeOfTextToReplace]
   let count = textFieldText.count - substringToReplace.count + string.count

   let allowedCharacters = ".-+1234567890"
   let allowedCharcterSet = CharacterSet(charactersIn: allowedCharacters)
   let typedCharcterSet = CharacterSet(charactersIn: string)

        if  allowedCharcterSet.isSuperset(of: typedCharcterSet)
                   , count <= 20 {
                 return true
              } else {
                 return false
              }
 }
如何使用它

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    // Verify all the conditions
    if let sdcTextField = textField as? JDTextField {
        return sdcTextField.verifyFields(shouldChangeCharactersIn: range, replacementString: string)
    } else {
        return true
    }
}
viewDidLoad()
中,只需在一行中设置特征

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.textField.delegate = self
    self.textField.maxLength = 10
    self.textField.allowedCharInString = ".-+1234567890"
}

你可以这样做

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

   guard let textFieldText = textField.text,
       let rangeOfTextToReplace = Range(range, in: textFieldText) else {
               return false
       }

   let substringToReplace = textFieldText[rangeOfTextToReplace]
   let count = textFieldText.count - substringToReplace.count + string.count

   let allowedCharacters = ".-+1234567890"
   let allowedCharcterSet = CharacterSet(charactersIn: allowedCharacters)
   let typedCharcterSet = CharacterSet(charactersIn: string)

        if  allowedCharcterSet.isSuperset(of: typedCharcterSet)
                   , count <= 20 {
                 return true
              } else {
                 return false
              }
 }
如何使用它

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    // Verify all the conditions
    if let sdcTextField = textField as? JDTextField {
        return sdcTextField.verifyFields(shouldChangeCharactersIn: range, replacementString: string)
    } else {
        return true
    }
}
viewDidLoad()
中,只需在一行中设置特征

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.textField.delegate = self
    self.textField.maxLength = 10
    self.textField.allowedCharInString = ".-+1234567890"
}

这回答了你的问题吗?这回答了你的问题吗?