No Description

UITextViewExt.swift 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. //adaptive height
  61. // if isAdaptiveHeight {
  62. var heightConstraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: height)
  63. for constraint in constraints {
  64. if constraint.firstAttribute == .height {
  65. heightConstraint = constraint
  66. }
  67. }
  68. let value = max(contentSize.height, heightConstraint.constant)
  69. heightConstraint.constant = value
  70. if !heightConstraint.isActive {
  71. heightConstraint.isActive = true
  72. }
  73. // }
  74. }
  75. /// Resize the placeholder UILabel to make sure it's in the same position as the UITextView text
  76. private func resizePlaceholder() {
  77. if let placeholderLabel = viewWithTag(1000001) as! UILabel? {
  78. let labelX = textContainer.lineFragmentPadding + textContainerInset.left
  79. let labelY = textContainerInset.top - 2
  80. let labelWidth = frame.width - (labelX * 2)
  81. let labelHeight = placeholderLabel.frame.height
  82. placeholderLabel.frame = CGRect(x: labelX, y: labelY, width: labelWidth, height: labelHeight)
  83. }
  84. }
  85. /// Add a placeholder UILabel to this UITextView
  86. private func addPlaceholder(_ placeholderText: String) {
  87. let placeholderLabel = UILabel()
  88. placeholderLabel.text = placeholderText
  89. placeholderLabel.sizeToFit()
  90. placeholderLabel.font = font
  91. placeholderLabel.textColor = UIColor.lightGray
  92. placeholderLabel.tag = 1000001
  93. placeholderLabel.isHidden = text.count > 0
  94. addSubview(placeholderLabel)
  95. resizePlaceholder()
  96. delegate = self
  97. }
  98. }
  99. // scroll
  100. extension UITextView {
  101. public func scrollToBottom() {
  102. let range = NSMakeRange(text.count - 1, 1)
  103. scrollRangeToVisible(range)
  104. }
  105. public func scrollToTop() {
  106. let range = NSMakeRange(0, 1)
  107. scrollRangeToVisible(range)
  108. }
  109. }