效果圖:
//MARK: -UITextViewDelegate
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") { // textView點擊完成隱藏鍵盤
textView.resignFirstResponder()
return false
}
//字數限制,在這里我的處理是給了一個簡單的提示,你也可以在這兒處理其他的顯示方式。
if range.location >= 30 {
SVProgressHUD.showErrorWithStatus("超過字數限制。", maskType: SVProgressHUDMaskType.Clear)
return false
}
return true
}
//swift2.3寫的,3.0的話只是語法的一點改動而已。