No Description

UITextViewExt.swift 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // UITextViewExt.swift
  3. // PaiaiUIKit
  4. //
  5. // Created by FFIB on 2017/9/13.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. //
  10. extension UITextView {
  11. public var isEmpty: Bool {
  12. return text?.isEmpty ?? false
  13. }
  14. public func clear() {
  15. text = ""
  16. attributedText = NSAttributedString(string: "")
  17. }
  18. }
  19. private var isLimit = false
  20. //placeholder
  21. extension UITextView: UITextViewDelegate {
  22. /// Resize the placeholder when the UITextView bounds change
  23. override open var bounds: CGRect {
  24. didSet {
  25. resizePlaceholder()
  26. }
  27. }
  28. open override var backgroundColor: UIColor? {
  29. didSet {
  30. if let placeholderLabel = viewWithTag(1000001) as? UILabel {
  31. placeholderLabel.backgroundColor = backgroundColor
  32. }
  33. }
  34. }
  35. public var placeholder: String? {
  36. get {
  37. var placeholderText: String?
  38. if let placeholderLabel = viewWithTag(1000001) as? UILabel {
  39. placeholderText = placeholderLabel.text
  40. }
  41. return placeholderText
  42. }
  43. set {
  44. if let placeholderLabel = viewWithTag(1000001) as! UILabel? {
  45. placeholderLabel.text = newValue
  46. placeholderLabel.sizeToFit()
  47. } else {
  48. self.addPlaceholder(newValue!)
  49. }
  50. delegate = self
  51. }
  52. }
  53. /// When the UITextView did change, show or hide the label based on if the UITextView is empty or not
  54. ///
  55. /// - Parameter textView: The UITextView that got updated
  56. public func textViewDidChange(_ textView: UITextView) {
  57. if let placeholderLabel = viewWithTag(1000001) as? UILabel {
  58. placeholderLabel.isHidden = text.count > 0
  59. }
  60. /// daptive height
  61. var heightConstraint = self.heightAnchor.constraint(equalToConstant: height)
  62. for constraint in constraints where constraint.firstAttribute == .height {
  63. heightConstraint = constraint
  64. }
  65. let value = max(contentSize.height, heightConstraint.constant)
  66. heightConstraint.constant = value
  67. if !heightConstraint.isActive {
  68. heightConstraint.isActive = true
  69. }
  70. }
  71. /// Resize the placeholder UILabel to make sure it's in the same position as the UITextView text
  72. private func resizePlaceholder() {
  73. if let placeholderLabel = viewWithTag(1000001) as! UILabel? {
  74. let labelX = textContainer.lineFragmentPadding + textContainerInset.left
  75. let labelY = textContainerInset.top - 2
  76. let labelWidth = frame.width - (labelX * 2)
  77. let labelHeight = placeholderLabel.frame.height
  78. placeholderLabel.frame = CGRect(x: labelX, y: labelY, width: labelWidth, height: labelHeight)
  79. }
  80. }
  81. /// Add a placeholder UILabel to this UITextView
  82. private func addPlaceholder(_ placeholderText: String) {
  83. let placeholderLabel = UILabel()
  84. placeholderLabel.text = placeholderText
  85. placeholderLabel.sizeToFit()
  86. placeholderLabel.font = font
  87. placeholderLabel.textColor = UIColor.lightGray
  88. placeholderLabel.tag = 1000001
  89. placeholderLabel.isHidden = text.count > 0
  90. addSubview(placeholderLabel)
  91. resizePlaceholder()
  92. delegate = self
  93. }
  94. }
  95. // scroll
  96. extension UITextView {
  97. public func scrollToBottom() {
  98. let range = NSRange(location: text.count - 1, length: 1)
  99. scrollRangeToVisible(range)
  100. }
  101. public func scrollToTop() {
  102. let range = NSRange(location: 0, length: 1)
  103. scrollRangeToVisible(range)
  104. }
  105. }