加入UITextViewDelegate
overridefuncviewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//設(shè)置UITextView
contentTextView.delegate=self
}
當(dāng)輸入框被改變時
functextViewDidChange(_textView:UITextView) {
lettextStrNum = textView.textasNSString
letViewWidth =Int(contentTextView.frame.width/20)
letlineNum =Int(textStrNum.length/ ViewWidth) +1
iftextStrNum.length>0{
contentTextViewplaceholder.isHidden=true
}else{
contentTextViewplaceholder.isHidden=false
}
if( lineNum <=3){
contentTextViewHeight.constant=105
}else{
contentTextViewHeight.constant=CGFloat(32.5*Double(lineNum))
}
}