Skip to content

Commit d348acd

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

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,30 @@ class AutogrowingTextView: UITextView {
3535
super.init(frame: frame, textContainer: textContainer)
3636
isScrollEnabled = false
3737

38+
setAutogrowing(allowAutogrowing)
39+
//TODO: enable only when line numbering is turned on
40+
contentMode = .redraw
41+
}
42+
43+
required init?(coder: NSCoder) {
44+
fatalError("init(coder:) has not been implemented")
45+
}
46+
47+
func setAutogrowing(_ isAutogrowing: Bool) {
48+
guard allowAutogrowing != isAutogrowing else { return }
49+
allowAutogrowing = isAutogrowing
50+
3851
if allowAutogrowing {
3952
heightAnchorConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: contentSize.height)
4053
heightAnchorConstraint.priority = .defaultHigh
4154

4255
NSLayoutConstraint.activate([
4356
heightAnchorConstraint
4457
])
58+
} else {
59+
isScrollEnabled = false
60+
NSLayoutConstraint.deactivate([heightAnchorConstraint])
4561
}
46-
//TODO: enable only when line numbering is turned on
47-
contentMode = .redraw
48-
}
49-
50-
required init?(coder: NSCoder) {
51-
fatalError("init(coder:) has not been implemented")
5262
}
5363

5464
override func layoutSubviews() {

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)