检查无效字符时获取UILabel的文本进行更改

时间:2018-10-04 18:17:32

标签: ios swift uitextfield uilabel

在用户名中使用无效字符时,我无法更改UILabel的文本。目前,其余代码可以正常工作-包括动画和振动。

这是我的代码:

func textFieldDidEndEditing(_ textField: UITextField) {

    let forbiddenUsernameCharacters = [".", " ", "#", "$", "[", "}", "]", "{", "(", "}"]

    guard let username = self.usernameTextField.text else { return }

    if textField == usernameTextField  && self.usernameTextField.text != "" {

        if !forbiddenUsernameCharacters.contains(where: username.contains) {
            print("Done")
            DispatchQueue.global(qos: .background).async {

                //Handle Global
                DispatchQueue.main.async {
                    self.checkForUserName(requestedUsername: self.usernameTextField.text!.lowercased())
                }
            }
        } else {
            self.errorLabel.text = "Invalid Characters"
            self.view.layoutIfNeeded()
            print("Invalid Characters")
            AudioServicesPlaySystemSound (1352)


            let animation = CABasicAnimation(keyPath: "position")
            animation.duration = 0.07
            animation.repeatCount = 4
            animation.autoreverses = true
            animation.fromValue = NSValue(cgPoint: CGPoint(x: self.usernameTextField.center.x - 10, y: self.usernameTextField.center.y))
            animation.toValue = NSValue(cgPoint: CGPoint(x: self.usernameTextField.center.x + 10, y: self.usernameTextField.center.y))

            self.usernameTextField.layer.add(animation, forKey: "position")
        }
    }
}

0 个答案:

没有答案