UITextInput+JChat.swift 648 B

12345678910111213141516171819202122232425262728
  1. //
  2. // UITextInput+JChat.swift
  3. // JChat
  4. //
  5. // Created by 邓永豪 on 2017/10/18.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. protocol TextProtocol {
  10. func limitNonMarkedTextSize(_ maxSize: Int)
  11. }
  12. extension UITextView: TextProtocol {
  13. func limitNonMarkedTextSize(_ maxSize: Int) {
  14. if self.markedTextRange != nil {
  15. return
  16. }
  17. let text = self.text!
  18. if text.count > maxSize {
  19. let range = (text.startIndex ..< text.index(text.startIndex, offsetBy: maxSize))
  20. let subText = text.substring(with: range)
  21. self.text = subText
  22. }
  23. }
  24. }