From d348acd5f8b13e781ede30e6d1b3b6f55d9f209f 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 | 22 ++++++++++++++----- Proton/Sources/Swift/Editor/EditorView.swift | 9 ++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Proton/Sources/Swift/Base/AutogrowingTextView.swift b/Proton/Sources/Swift/Base/AutogrowingTextView.swift index de23c6b6..839f83b8 100644 --- a/Proton/Sources/Swift/Base/AutogrowingTextView.swift +++ b/Proton/Sources/Swift/Base/AutogrowingTextView.swift @@ -35,6 +35,19 @@ class AutogrowingTextView: UITextView { super.init(frame: frame, textContainer: textContainer) isScrollEnabled = false + setAutogrowing(allowAutogrowing) + //TODO: enable only when line numbering is turned on + contentMode = .redraw + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func setAutogrowing(_ isAutogrowing: Bool) { + guard allowAutogrowing != isAutogrowing else { return } + allowAutogrowing = isAutogrowing + if allowAutogrowing { heightAnchorConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height) heightAnchorConstraint.priority = .defaultHigh @@ -42,13 +55,10 @@ class AutogrowingTextView: UITextView { NSLayoutConstraint.activate([ heightAnchorConstraint ]) + } else { + isScrollEnabled = false + NSLayoutConstraint.deactivate([heightAnchorConstraint]) } - //TODO: enable only when line numbering is turned on - contentMode = .redraw - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") } override func layoutSubviews() { 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) }