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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
//
// CustomTextView.swift
// MullvadVPN
//
// Created by pronebird on 16/09/2020.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import UIKit
class CustomTextView: UITextView {
private static let textViewCornerRadius: CGFloat = 4
var roundCorners = true {
didSet {
layer.cornerRadius = roundCorners ? Self.textViewCornerRadius : 0
}
}
/// Placeholder string
var placeholder: String? {
get {
placeholderTextLabel.text
}
set {
placeholderTextLabel.text = newValue
}
}
/// Placeholder text label
private let placeholderTextLabel = UILabel()
/// Placeholder label constraints
private var placeholderConstraints = [NSLayoutConstraint]()
override var textContainerInset: UIEdgeInsets {
didSet {
setNeedsUpdateConstraints()
}
}
override var font: UIFont? {
didSet {
placeholderTextLabel.font = font ?? .mullvadSmall
}
}
/// Placeholder text inset derived from `textContainerInset`
private var placeholderTextInset: UIEdgeInsets {
var placeholderInset = textContainerInset
// Offset the placeholder label to match with text view rendering.
placeholderInset.top += 0.5
return placeholderInset
}
override var accessibilityLabel: String? {
get {
if self.text.isEmpty {
return placeholderTextLabel.text
} else {
return super.accessibilityLabel
}
}
set {
super.accessibilityLabel = newValue
}
}
override var accessibilityPath: UIBezierPath? {
get {
if roundCorners {
return UIBezierPath(
roundedRect: accessibilityFrame,
cornerRadius: Self.textViewCornerRadius
)
} else {
return UIBezierPath(rect: accessibilityFrame)
}
}
set {
super.accessibilityPath = newValue
}
}
nonisolated(unsafe) private var notificationObserver: Any?
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
placeholderTextLabel.isAccessibilityElement = false
placeholderTextLabel.accessibilityTraits = []
placeholderTextLabel.textColor = UIColor.TextField.placeholderTextColor
placeholderTextLabel.highlightedTextColor = UIColor.TextField.placeholderTextColor
placeholderTextLabel.translatesAutoresizingMaskIntoConstraints = false
placeholderTextLabel.adjustsFontForContentSizeCategory = true
placeholderTextLabel.numberOfLines = 0
addSubview(placeholderTextLabel)
// Create placeholder constraints
placeholderConstraints = [
placeholderTextLabel.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
placeholderTextLabel.leadingAnchor
.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
placeholderTextLabel.trailingAnchor
.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor),
placeholderTextLabel.bottomAnchor
.constraint(lessThanOrEqualTo: safeAreaLayoutGuide.bottomAnchor),
]
NSLayoutConstraint.activate(placeholderConstraints)
// Set visual appearance
textColor = UIColor.TextField.textColor
layer.cornerRadius = Self.textViewCornerRadius
clipsToBounds = true
// Set content padding
contentInset = .zero
textContainerInset = UIEdgeInsets(top: 12, left: 14, bottom: 12, right: 14)
self.textContainer.lineFragmentPadding = 0
// Handle placeholder visibility
notificationObserver = NotificationCenter.default.addObserver(
forName: NSTextStorage.didProcessEditingNotification,
object: textStorage,
queue: OperationQueue.main
) { [weak self] _ in
MainActor.assumeIsolated {
self?.updatePlaceholderVisibility()
}
}
updatePlaceholderVisibility()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
if let notificationObserver {
NotificationCenter.default.removeObserver(notificationObserver)
}
}
override func updateConstraints() {
let textInset = placeholderTextInset
for constraint in placeholderConstraints {
switch constraint.firstAttribute {
case .top:
constraint.constant = textInset.top
case .leading:
constraint.constant = textInset.left
case .trailing:
constraint.constant = -textInset.right
case .bottom:
constraint.constant = -textInset.bottom
default:
break
}
}
super.updateConstraints()
}
private func updatePlaceholderVisibility() {
placeholderTextLabel.isHidden = textStorage.length > 0
}
}
|