快速创建和自定义Alertview内部的UITextfield

我正在尝试通过编程方式在警报视图内创建一个大尺寸文本字段,限制为100个字符。这是我的代码:

func alertWithTF() {
        let alert = UIAlertController(title: Constants.cancelPopupTitle,message: Constants.cancelPopupMessage,preferredStyle: .alert)
        alert.addTextField { (textField) in
            textField.frame = CGRect(x: 20,y: 100,width: 300,height: 80)
            textField.placeholder = "Please enter message"
            textField.borderStyle = UITextField.BorderStyle.line
            textField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
        }
        alert.addaction(UIAlertaction(title: "OK",style: .default,handler: { [weak alert] (_) in
            guard let textfield = alert?.textFields?.first else { return }
            let message = textfield.text ?? ""
            print(message)
        }))
        alert.addaction(UIAlertaction(title: "Cancel",style: .cancel))
        self.present(alert,animated: true,completion: nil)
}

这是我的问题:

  • 我想通过编辑文本框的框架来修改文本框的高度,但这似乎不起作用。
  • 我还想设置文本字段的限制字符

我可以使用任何可行的解决方案吗?感谢您的帮助。

MELIDY 回答:快速创建和自定义Alertview内部的UITextfield

  1. UIAlertController是不可自定义的。您可以为这种情况创建自己的alertViewController,然后根据需要使用UI。

  2. 在您的addTextfield中,提供对self的委托

      alert.addTextField { (textField) in
    
        textField.frame = CGRect(x: 20,y: 100,width: 300,height: 80)
        textField.placeholder = "Please enter message"
        textField.borderStyle = UITextField.BorderStyle.line
        textField.delegate = self
        textField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
    }
    

然后您可以使用shouldChangeCharactersIn方法来限制长度

func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) -> Bool {
let maxLimit = 180
let currentString = textField.text!
let newString: String =
    currentString.replacingCharacters(in: range,with: string) as String
return newString.length <= maxLength
}

确保您的ViewController符合UITextFieldDelegate。

class YourViewController: UIViewController,UITextFieldDelegate {

}
,
alert.addTextField { textField in
    let heightConstraint = NSLayoutConstraint(item: textField,attribute: .height,relatedBy: .equal,toItem: nil,attribute: .notAnAttribute,multiplier: 1,constant: 100)
    textField.addConstraint(heightConstraint)
}
,

因此,经过我的一些研究,我设法实现了这一点:

extension ViewController: UITextViewDelegate {
    func alertWithTF() {
        let alert = UIAlertController(title: "Title",message: "\n\n\n\n\n",preferredStyle: .alert)
        let textView = UITextView()
        textView.borderColor = .black
        textView.borderWidth = 1.0
        textView.delegate = self
        textView.text = Constants.textViewPlaceholder
        textView.textColor = .lightGray
        alert.view.addSubview(textView)

        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.topAnchor.constraint(equalTo: alert.view.topAnchor,constant: 125).isActive = true
        textView.rightAnchor.constraint(equalTo: alert.view.rightAnchor,constant: -20).isActive = true
        textView.leftAnchor.constraint(equalTo: alert.view.leftAnchor,constant: 20).isActive = true
        textView.bottomAnchor.constraint(equalTo: alert.view.bottomAnchor,constant: -60).isActive = true
        textView.heightAnchor.constraint(equalToConstant: 60).isActive = true

        alert.view.translatesAutoresizingMaskIntoConstraints = false

        alert.addAction(UIAlertAction(title: "Cancel",style: .default,handler: { [weak self] (_) in
            self?.dismiss(animated: true,completion: nil)
        }))

        alert.addAction(UIAlertAction(title: "OK",handler: { [weak self] (_) in
            let message = (textView.text == nil||textView.text == Constants.textViewPlaceholder ? "" : textView.text)
            print(message)
        }))

        self.present(alert,animated: true,completion: nil)
    }

所以我创建了一个TextView而不是TextField,并在alertView中为其设置了约束。这些代码不是完美的,但是效果很好。我也使用TextView委托来处理textView字符限制a和占位符,因为UITextView不支持它:

func textView(_ textView: UITextView,shouldChangeTextIn range: NSRange,replacementText text: String) -> Bool {
        let newText = (textView.text as NSString).replacingCharacters(in: range,with: text)
        let numberOfChars = newText.count
        return numberOfChars < 101 //Max 100 chars
    }

    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.textColor == UIColor.lightGray {
            textView.text = ""
            textView.textColor = UIColor.black
        }
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text == "" {
            textView.text = Constants.textViewPlaceholder
            textView.textColor = UIColor.lightGray
        }
    }

希望这会有所帮助!

本文链接:https://www.f2er.com/2963902.html

大家都在问