如何限制 2 文本视图中的字符数 Swift 在同一时间?

标签: UITextView ios Swift
发布时间: 2016/5/8 2:05:54
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一些问题想要设置的 2 个字符的数字不同 UITextView s 到 12 (例如)。

它仅有一个工作还 OK UITextView ,但那是很难与两个。 在他们写入文本时有实际上一些错误。它现在不键入任何内容。我一直在不同的方式,就能做到,但一切似乎都错在 shouldChangeTextInRange 方法。

如何正确设置两个 textViews 字符的数?

class viewControllerCuatro: UIViewController, UITextViewDelegate {

    @IBOutlet weak var textViewInteriorUno: UITextView!
    @IBOutlet weak var textViewInteriorDos: UITextView!

    @IBOutlet weak var textViewExteriorUno: UITextView!
    @IBOutlet weak var textViewExteriorDos: UITextView!

    @IBOutlet weak var foto1: UIImageView!
    @IBOutlet weak var foto2: UIImageView!

    @IBOutlet weak var imagenFondo: UIImageView!

    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() { 

        textViewExteriorUno.delegate = self
        textViewExteriorDos.delegate = self
        textViewInteriorUno.delegate = self
        textViewInteriorDos.delegate = self


        textViewExteriorUno.layer.masksToBounds = true
        textViewExteriorUno.layer.borderWidth = 6
        textViewExteriorUno.layer.cornerRadius = textViewExteriorUno.layer.frame.height/6
        textViewExteriorUno.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor


        textViewExteriorDos.layer.masksToBounds = true
        textViewExteriorDos.layer.borderWidth = 6
        textViewExteriorDos.layer.cornerRadius = textViewExteriorDos.layer.frame.height/6
        textViewExteriorDos.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor


        foto1.layer.masksToBounds = true
        foto1.layer.borderWidth = 6
        foto1.layer.cornerRadius = foto1.layer.frame.height/6
        foto1.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor


        foto2.layer.masksToBounds = true
        foto2.layer.borderWidth = 6
        foto2.layer.cornerRadius = foto2.layer.frame.height/6
        foto2.layer.borderColor = UIColor.purpleColor().colorWithAlphaComponent(0.2).CGColor

        foto1.image = fotoUnoEscogida
        foto2.image = fotoDosEscogida

        imagenFondo.image = UIImage (named: tipoDeHojaElegida) 
    }


    override func viewDidAppear(animated: Bool) {

        let alertNotSuccessRegister = UIAlertController(title: "¡Casi hemos terminado!", message: "Modifica los textos a tu gusto para que sea lo más personal posible.", preferredStyle: .ActionSheet)

        alertNotSuccessRegister.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))

        self.presentViewController(alertNotSuccessRegister, animated: true, completion: nil)
    }

    @IBAction func botonSiguiente(sender: AnyObject) {
        if textViewInteriorUno.text != " " || textViewInteriorDos.text != " " {

            textoPersonalUno = textViewInteriorUno.text!
            textoPersonalDos = textViewInteriorDos.text!

            performSegueWithIdentifier("hey3", sender: self)

                  print("Se acaba de guardar el texto \(textoPersonalDos)")

        } else {

            let alertaError = UIAlertController(title: "Por favor, rellena los campos de texto.", message: "Dedica unas bonitas palabras :D", preferredStyle: .ActionSheet)

            alertaError.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))

            self.presentViewController(alertaError, animated: true, completion: nil) 
        }
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        if textViewInteriorUno.text == ""  || textViewInteriorUno.text.isEmpty == true || textViewInteriorDos.text == "" || textViewInteriorDos.text.isEmpty == true {

            textViewInteriorUno.text = " "
            textViewInteriorUno.resignFirstResponder()

            textViewInteriorDos.text = " "
            textViewInteriorDos.resignFirstResponder()
        }
        else{

            self.textViewInteriorUno.resignFirstResponder()

            self.textViewInteriorDos.resignFirstResponder()
        }
    }

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{

        let newLength = textViewInteriorUno.text.utf16.count + text.utf16.count - range.length

        let otherLength = textViewInteriorDos.text.utf16.count + text.utf16.count - range.length

        if (newLength <= 12) && (otherLength > 6) {
            return true
        } else {
            return false
        }
    }

解决方法 1:

委托方法是告诉哪些文本视图委托被调用。所以,看在该方法中的 textView 参数,然后返回基础上的。例如 ︰

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{

    var shouldReplace = true // For most cases, return true, only modify if we have a special case
      let newLength = textView.text.utf16.count + text.utf16.count - range.length
    // If the textView is 'textViewInteriorUno'
    if textView.isEqual(textViewInteriorUno
    {
      shouldReplace = newLength <= 12           // will be true if the length is less than or equal to 12
    }
    // If the textView is 'textViewInteriorDos'
    else if textView.isEqual(textViewInteriorDos) 
    {
      shouldReplace = newLength < 6 // Will be true if the length is less then 6
   }

    return shouldReplace
}

这种方式,您基于您的代理响应 textView 是什么,和你不要修改其他视图的行为。

官方微信
官方QQ群
31647020