Created
January 23, 2020 02:37
-
-
Save mrugeshtank/ea6a0c23ee951305c5a7366c3ba68a93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private var __maxLengthsForTextView = [UITextView: Int]() | |
private var kAssociationKeyMaxLengthTextView: Int = 0 | |
extension UITextView:UITextViewDelegate { | |
@IBInspectable var maxLength: Int { | |
get { | |
if let length = objc_getAssociatedObject(self, &kAssociationKeyMaxLengthTextView) as? Int { | |
return length | |
} else { | |
return Int.max | |
} | |
} | |
set { | |
self.delegate = self | |
objc_setAssociatedObject(self, &kAssociationKeyMaxLengthTextView, newValue, .OBJC_ASSOCIATION_RETAIN) | |
} | |
} | |
/*public func textViewDidChange(_ textView: UITextView) { | |
checkMaxLength(textField: self) | |
}*/ | |
@objc func checkMaxLength(textField: UITextView) { | |
guard let prospectiveText = self.text, | |
prospectiveText.count > maxLength | |
else { | |
return | |
} | |
let selection = selectedTextRange | |
let indexEndOfText = prospectiveText.index(prospectiveText.startIndex, offsetBy: maxLength) | |
let substring = prospectiveText[..<indexEndOfText] | |
text = String(substring) | |
selectedTextRange = selection | |
} | |
/// Resize the placeholder when the UITextView bounds change | |
override open var bounds: CGRect { | |
didSet { | |
self.resizePlaceholder() | |
} | |
} | |
/// The UITextView placeholder text | |
public var placeholder: String? { | |
get { | |
var placeholderText: String? | |
if let placeholderLabel = self.viewWithTag(100) as? UILabel { | |
placeholderText = placeholderLabel.text | |
} | |
return placeholderText | |
} | |
set { | |
if let placeholderLabel = self.viewWithTag(100) as! UILabel? { | |
placeholderLabel.text = newValue | |
placeholderLabel.sizeToFit() | |
} else { | |
self.addPlaceholder(newValue!) | |
} | |
} | |
} | |
/// When the UITextView did change, show or hide the label based on if the UITextView is empty or not | |
/// | |
/// - Parameter textView: The UITextView that got updated | |
public func textViewDidChange(_ textView: UITextView) { | |
if let placeholderLabel = self.viewWithTag(100) as? UILabel { | |
placeholderLabel.isHidden = self.text.count > 0 | |
} | |
checkMaxLength(textField: self) | |
} | |
func checkPlaceholder() { | |
if let placeholderLabel = self.viewWithTag(100) as? UILabel { | |
placeholderLabel.isHidden = self.text.count > 0 | |
} | |
checkMaxLength(textField: self) | |
} | |
/// Resize the placeholder UILabel to make sure it's in the same position as the UITextView text | |
private func resizePlaceholder() { | |
if let placeholderLabel = self.viewWithTag(100) as! UILabel? { | |
let labelX = self.textContainer.lineFragmentPadding | |
let labelY = self.textContainerInset.top - 2 | |
let labelWidth = self.frame.width - (labelX * 2) | |
let labelHeight = placeholderLabel.frame.height | |
placeholderLabel.frame = CGRect(x: labelX, y: labelY, width: labelWidth, height: labelHeight) | |
} | |
} | |
/// Adds a placeholder UILabel to this UITextView | |
private func addPlaceholder(_ placeholderText: String) { | |
let placeholderLabel = UILabel() | |
placeholderLabel.text = placeholderText | |
placeholderLabel.sizeToFit() | |
placeholderLabel.font = self.font | |
placeholderLabel.textColor = UIColor.lightGray | |
placeholderLabel.tag = 100 | |
placeholderLabel.isHidden = self.text.count > 0 | |
self.addSubview(placeholderLabel) | |
self.resizePlaceholder() | |
self.delegate = self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment