swift3 限制text View只能輸入10個字,無法輸入第11個字(用到shouldChangeTextIn,textViewDidChange function)

different from the textfiled project
 this project use textViewDidChange function

import UIKit

class ViewController: UIViewController,UITextViewDelegate {

@IBOutlet weak var overLabel: UILabel!
@IBOutlet weak var textview: UITextView!
@IBOutlet weak var showLabel: UILabel!
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text != "" && (textview.text.characters.count) < 10 {
overLabel.isHidden = true

return true
}
else if text == ""{
overLabel.isHidden = false
overLabel.text = "輸入非字母或數字不列入字數計算"

return true
}
else{
overLabel.isHidden = false
overLabel.text = "不能輸入第11個字母"
return false
}

}

func textViewDidChange(_ textView: UITextView) {
showLabel.isHidden = false
showLabel.text = String(textview.text.characters.count)
}
override func viewDidLoad() {
super.viewDidLoad()
showLabel.isHidden = true
overLabel.isHidden = true
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

Originally published at swifthao的ios學習紀錄blog.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.