PQTextView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // PQTextView.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/6/11.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. open class PQTextView: UITextView {
  10. private let textValuesQueue = DispatchQueue(label: "com.BytesFlow.textValuesQueue", qos: .default)
  11. public var textDidChangedHandle: ((_ textView: UITextView) -> Void)?
  12. // 最多输入的个数
  13. public var maxTextLength : Int?
  14. // 输入个数超过时提示
  15. public var maxTextLengthRemind : String?
  16. // add by ak 默认字的起点位置
  17. public var placeHolderDefultPoint = CGPoint.init(x: 5, y: 7)
  18. /// setNeedsDisplay调用drawRect
  19. public var placeHolder: String = "" {
  20. didSet {
  21. setNeedsDisplay()
  22. }
  23. }
  24. /// placeHolder是否居中
  25. public var isCenter: Bool = false {
  26. didSet {
  27. setNeedsDisplay()
  28. }
  29. }
  30. public var placeHolderColor: UIColor = UIColor.gray {
  31. didSet {
  32. setNeedsDisplay()
  33. }
  34. }
  35. open override var font: UIFont? {
  36. didSet {
  37. setNeedsDisplay()
  38. }
  39. }
  40. open override var text: String! {
  41. didSet{
  42. setNeedsLayout()
  43. }
  44. }
  45. open override var attributedText: NSAttributedString! {
  46. didSet {
  47. setNeedsDisplay()
  48. }
  49. }
  50. public override init(frame: CGRect, textContainer: NSTextContainer?) {
  51. super.init(frame: frame, textContainer: textContainer)
  52. /// default字号
  53. PQNotification.addObserver(self, selector: #selector(textDidChanged(noti:)), name: UITextView.textDidChangeNotification, object: self)
  54. }
  55. required public init?(coder _: NSCoder) {
  56. fatalError("init(coder:) has not been implemented")
  57. }
  58. @objc func textDidChanged(noti _: NSNotification) {
  59. if maxTextLength != nil && text.count > maxTextLength! {
  60. text = String(text.prefix(maxTextLength!))
  61. cShowHUB(superView: nil, msg: (maxTextLengthRemind != nil && (maxTextLengthRemind?.count ?? 0) > 0) ? maxTextLengthRemind : "最多输入\( maxTextLength!)个字符")
  62. }
  63. setNeedsDisplay()
  64. if textDidChangedHandle != nil {
  65. textDidChangedHandle!(self)
  66. }
  67. }
  68. open override func draw(_ rect: CGRect) {
  69. if hasText {
  70. return
  71. }
  72. let size = sizeWithText(text: placeHolder, font: font ?? UIFont.systemFont(ofSize: 14), size: rect.size)
  73. var newRect = CGRect()
  74. newRect.size.width = size.width
  75. newRect.size.height = size.height
  76. if isCenter {
  77. newRect.origin.x = (rect.width - size.width) / 2
  78. newRect.origin.y = (rect.height - size.height) / 2
  79. } else {
  80. newRect.origin.x = placeHolder.contains("未识别到文字") ? 0 : placeHolderDefultPoint.x
  81. newRect.origin.y = placeHolder.contains("未识别到文字") ? 0 : placeHolderDefultPoint.y
  82. }
  83. (placeHolder as NSString).draw(in: newRect, withAttributes: [NSAttributedString.Key.font: font ?? UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: placeHolderColor])
  84. }
  85. open override func layoutSubviews() {
  86. super.layoutSubviews()
  87. setNeedsDisplay()
  88. }
  89. deinit {
  90. PQNotification.removeObserver(self, name: UITextView.textDidChangeNotification, object: self)
  91. }
  92. }