Skip to content

Commit a134e7d

Browse files
authored
Change scroll enabled (#347)
* Allow changing allowAutogrowing after init * Recalculate editor height on setting back scrollable
1 parent cb7e212 commit a134e7d

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

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

+30-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+
addHeightConstraint()
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,34 @@ 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+
addHeightConstraint()
53+
recalculateHeight()
54+
}
55+
} else {
56+
isScrollEnabled = false
57+
if let heightAnchorConstraint {
58+
NSLayoutConstraint.deactivate([heightAnchorConstraint])
59+
}
60+
heightAnchorConstraint = nil
61+
}
62+
}
63+
64+
private func addHeightConstraint() {
65+
guard allowAutogrowing else { return }
66+
let heightConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height)
67+
heightAnchorConstraint = heightConstraint
68+
heightAnchorConstraint?.priority = .defaultHigh
69+
70+
NSLayoutConstraint.activate([
71+
heightConstraint
72+
])
73+
}
74+
5475
override func layoutSubviews() {
5576
super.layoutSubviews()
5677
guard allowAutogrowing, maxHeight != .greatestFiniteMagnitude else { return }

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

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

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

0 commit comments

Comments
 (0)