From 5eb9d29c0c821297f9aa001badcd4e26556ad3e8 Mon Sep 17 00:00:00 2001 From: Rajdeep Kwatra Date: Mon, 23 Sep 2024 14:13:53 +1000 Subject: [PATCH] Allow changing allowAutogrowing after init --- .../Swift/Base/AutogrowingTextView.swift | 33 ++++++++++++++----- Proton/Sources/Swift/Editor/EditorView.swift | 9 +++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Proton/Sources/Swift/Base/AutogrowingTextView.swift b/Proton/Sources/Swift/Base/AutogrowingTextView.swift index de23c6b6..5f396816 100644 --- a/Proton/Sources/Swift/Base/AutogrowingTextView.swift +++ b/Proton/Sources/Swift/Base/AutogrowingTextView.swift @@ -27,22 +27,15 @@ class AutogrowingTextView: UITextView { private var allowAutogrowing: Bool weak var boundsObserver: BoundsObserving? private var maxHeightConstraint: NSLayoutConstraint! - private var heightAnchorConstraint: NSLayoutConstraint! + private var heightAnchorConstraint: NSLayoutConstraint? private var isSizeRecalculationRequired = true init(frame: CGRect = .zero, textContainer: NSTextContainer? = nil, allowAutogrowing: Bool = false) { self.allowAutogrowing = allowAutogrowing super.init(frame: frame, textContainer: textContainer) isScrollEnabled = false + setAutogrowing(allowAutogrowing) - if allowAutogrowing { - heightAnchorConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height) - heightAnchorConstraint.priority = .defaultHigh - - NSLayoutConstraint.activate([ - heightAnchorConstraint - ]) - } //TODO: enable only when line numbering is turned on contentMode = .redraw } @@ -51,6 +44,28 @@ class AutogrowingTextView: UITextView { fatalError("init(coder:) has not been implemented") } + func setAutogrowing(_ isAutogrowing: Bool) { + allowAutogrowing = isAutogrowing + + if allowAutogrowing { + if heightAnchorConstraint == nil { + let heightConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height) + heightAnchorConstraint = heightConstraint + heightAnchorConstraint?.priority = .defaultHigh + + NSLayoutConstraint.activate([ + heightConstraint + ]) + } + } else { + isScrollEnabled = false + if let heightAnchorConstraint { + NSLayoutConstraint.deactivate([heightAnchorConstraint]) + } + heightAnchorConstraint = nil + } + } + override func layoutSubviews() { super.layoutSubviews() guard allowAutogrowing, maxHeight != .greatestFiniteMagnitude else { return } diff --git a/Proton/Sources/Swift/Editor/EditorView.swift b/Proton/Sources/Swift/Editor/EditorView.swift index 8db06e14..0c31801a 100644 --- a/Proton/Sources/Swift/Editor/EditorView.swift +++ b/Proton/Sources/Swift/Editor/EditorView.swift @@ -1306,6 +1306,15 @@ open class EditorView: UIView { richTextView.recalculateHeight(size: size) } + /// Set the behavior for how Editor size would be updated based on content + /// - Parameter isAutogrowing: When `true`, uses custom calculation and constrains to size editor based on content. This is typically the case where + /// Editor is scrollable and needs to be confined to certain size using applied constraints. Use `false` in case Editor is itself non-scrollable but is hosted within + /// another scroll container. This will use iOS's internal logic for sizing the Editor based on the height of the content and is generally better performing. + public func setAutogrowing(_ isAutogrowing: Bool) { + richTextView.setAutogrowing(isAutogrowing) + } + + open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { return richTextView.canPerformAction(action, withSender: sender) }