Commit 9fae5e1f by Orkhan Alikhanov Committed by OrkhanAlikhanov

Added preferredMaxLayoutHeight to TextView

parent 9cd30b33
...@@ -263,6 +263,12 @@ open class TextView: UITextView { ...@@ -263,6 +263,12 @@ open class TextView: UITextView {
layoutShape() layoutShape()
layoutShadowPath() layoutShadowPath()
layoutPlaceholderLabel() layoutPlaceholderLabel()
guard isGrowEnabled else {
return
}
invalidateIntrinsicContentSize()
} }
/** /**
...@@ -283,18 +289,43 @@ open class TextView: UITextView { ...@@ -283,18 +289,43 @@ open class TextView: UITextView {
prepareRegularExpression() prepareRegularExpression()
preparePlaceholderLabel() preparePlaceholderLabel()
} }
open override func insertText(_ text: String) { /// Maximum preffered layout height before scrolling.
fixTypingFont() open var preferredMaxLayoutHeight: CGFloat = 0 {
super.insertText(text) didSet {
fixTypingFont() invalidateIntrinsicContentSize()
} }
}
open override func paste(_ sender: Any?) {
fixTypingFont() /// A property indicating if textView allowed to grow.
super.paste(sender) private var isGrowEnabled: Bool {
fixTypingFont() return preferredMaxLayoutHeight > 0 && isScrollEnabled
}
open override var intrinsicContentSize: CGSize {
guard isGrowEnabled else {
return super.intrinsicContentSize
} }
let insets = textContainerInsets
let w = bounds.width - insets.left - insets.right - 2 * textContainer.lineFragmentPadding
let placeholderH = placeholderLabel.sizeThatFits(CGSize(width: w, height: .greatestFiniteMagnitude)).height
let h = max(contentSize.height, placeholderH + insets.top + insets.bottom)
return CGSize(width: UIView.noIntrinsicMetric, height: min(h, preferredMaxLayoutHeight))
}
open override func insertText(_ text: String) {
fixTypingFont()
super.insertText(text)
fixTypingFont()
}
open override func paste(_ sender: Any?) {
fixTypingFont()
super.paste(sender)
fixTypingFont()
}
} }
fileprivate extension TextView { fileprivate extension TextView {
......
...@@ -129,6 +129,7 @@ open class Toolbar: Bar { ...@@ -129,6 +129,7 @@ open class Toolbar: Bar {
prepareDetailLabel() prepareDetailLabel()
} }
/// A reference to titleLabel.textAlignment observation.
private var titleLabelTextAlignmentObserver: NSKeyValueObservation! private var titleLabelTextAlignmentObserver: NSKeyValueObservation!
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment