Skip to content

Commit ce936e8

Browse files
committed
Allow changing allowAutogrowing after init
1 parent 46db750 commit ce936e8

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

Diff for: Proton/Sources/Swift/Base/AutogrowingTextView.swift

+23-9
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,15 @@ class AutogrowingTextView: UITextView {
2727
private var allowAutogrowing: Bool
2828
weak var boundsObserver: BoundsObserving?
2929
private var maxHeightConstraint: NSLayoutConstraint!
30-
private var heightAnchorConstraint: NSLayoutConstraint!
30+
private var heightAnchorConstraint: NSLayoutConstraint?
3131
private var isSizeRecalculationRequired = true
3232

3333
init(frame: CGRect = .zero, textContainer: NSTextContainer? = nil, allowAutogrowing: Bool = false) {
3434
self.allowAutogrowing = allowAutogrowing
3535
super.init(frame: frame, textContainer: textContainer)
3636
isScrollEnabled = false
37+
setAutogrowing(allowAutogrowing)
3738

38-
if allowAutogrowing {
39-
heightAnchorConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height)
40-
heightAnchorConstraint.priority = .defaultHigh
41-
42-
NSLayoutConstraint.activate([
43-
heightAnchorConstraint
44-
])
45-
}
4639
//TODO: enable only when line numbering is turned on
4740
contentMode = .redraw
4841
}
@@ -51,6 +44,27 @@ class AutogrowingTextView: UITextView {
5144
fatalError("init(coder:) has not been implemented")
5245
}
5346

47+
func setAutogrowing(_ isAutogrowing: Bool) {
48+
allowAutogrowing = isAutogrowing
49+
50+
if allowAutogrowing {
51+
if heightAnchorConstraint == nil {
52+
let heightConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height)
53+
heightAnchorConstraint = heightConstraint
54+
heightAnchorConstraint?.priority = .defaultHigh
55+
56+
NSLayoutConstraint.activate([
57+
heightConstraint
58+
])
59+
}
60+
} else {
61+
isScrollEnabled = false
62+
if let heightAnchorConstraint {
63+
NSLayoutConstraint.deactivate([heightAnchorConstraint])
64+
}
65+
}
66+
}
67+
5468
override func layoutSubviews() {
5569
super.layoutSubviews()
5670
guard allowAutogrowing, maxHeight != .greatestFiniteMagnitude else { return }

Diff for: Proton/Sources/Swift/Editor/EditorView.swift

+9
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,15 @@ open class EditorView: UIView {
13061306
richTextView.recalculateHeight(size: size)
13071307
}
13081308

1309+
/// Set the behavior for how Editor size would be updated based on content
1310+
/// - Parameter isAutogrowing: When `true`, uses custom calculation and constrains to size editor based on content. This is typically the case where
1311+
/// 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
1312+
/// 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.
1313+
public func setAutogrowing(_ isAutogrowing: Bool) {
1314+
richTextView.setAutogrowing(isAutogrowing)
1315+
}
1316+
1317+
13091318
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
13101319
return richTextView.canPerformAction(action, withSender: sender)
13111320
}

0 commit comments

Comments
 (0)