-
Notifications
You must be signed in to change notification settings - Fork 83
/
AutogrowingTextView.swift
110 lines (93 loc) · 4.45 KB
/
AutogrowingTextView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// AutogrowingTextView.swift
// Proton
//
// Created by Rajdeep Kwatra on 31/12/19.
// Copyright © 2019 Rajdeep Kwatra. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UIKit
class AutogrowingTextView: UITextView {
var maxHeight: CGFloat = 0
private var allowAutogrowing: Bool
weak var boundsObserver: BoundsObserving?
private var maxHeightConstraint: 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
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
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
guard allowAutogrowing, maxHeight != .greatestFiniteMagnitude else { return }
// Required to reset the size if content is removed
if contentSize.height <= frame.height {
recalculateHeight()
invalidateIntrinsicContentSize()
return
}
guard isSizeRecalculationRequired else { return }
isSizeRecalculationRequired = false
recalculateHeight()
}
func recalculateHeight(size: CGSize? = nil) {
guard allowAutogrowing else { return }
let bounds = self.bounds.integral
let sizeToUse = size ?? frame.size
let fittingSize = self.calculatedSize(attributedText: attributedText, frame: sizeToUse, textContainerInset: textContainerInset)
self.isScrollEnabled = (fittingSize.height > bounds.height) || (self.maxHeight > 0 && self.maxHeight < fittingSize.height)
self.heightAnchorConstraint?.constant = min(fittingSize.height, self.contentSize.height)
}
override open func sizeThatFits(_ size: CGSize) -> CGSize {
var fittingSize = calculatedSize(attributedText: attributedText, frame: size, textContainerInset: textContainerInset)
if maxHeight > 0 {
fittingSize.height = min(maxHeight, fittingSize.height)
}
return fittingSize
}
override var bounds: CGRect {
didSet {
guard ceil(oldValue.height) != ceil(bounds.height) else { return }
boundsObserver?.didChangeBounds(bounds, oldBounds: oldValue)
isSizeRecalculationRequired = true
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.becomeFirstResponder()
}
private func calculatedSize(attributedText: NSAttributedString, frame: CGSize, textContainerInset: UIEdgeInsets) -> CGSize {
DispatchQueue.global(qos: .userInteractive).sync { [lineFragmentPadding = textContainer.lineFragmentPadding ] in
// Adjust for horizontal paddings in textview to exclude from overall available width for attachment
let horizontalAdjustments = (lineFragmentPadding * 2) + (textContainerInset.left + textContainerInset.right)
let boundingRect = attributedText.boundingRect(with: CGSize(width: frame.width - horizontalAdjustments, height: .greatestFiniteMagnitude), options: [.usesLineFragmentOrigin], context: nil).integral
let insets = UIEdgeInsets(top: -textContainerInset.top, left: -textContainerInset.left, bottom: -textContainerInset.bottom, right: -textContainerInset.right)
return boundingRect.inset(by: insets).size
}
}
}