Skip to content

Commit 5b47591

Browse files
committed
Support autocapitalizationType for iOS
AppKit doesn't have such a setting for NSTextView, so I don't support it for macOS.
1 parent 6f01260 commit 5b47591

3 files changed

Lines changed: 33 additions & 16 deletions

File tree

Example/Example (Shared)/ContentView.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct ContentView: View {
1212
@State var text1 = ""
1313
@State var text2 = ""
1414
@State var text3 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
15+
@State var text4 = ""
1516

1617
var body: some View {
1718
List {
@@ -65,6 +66,14 @@ struct ContentView: View {
6566
hasGreedyWidth: false)
6667
.background(textBackgroundColor)
6768
}
69+
70+
Section("No autocapitalization") {
71+
ResizingTextView(
72+
text: $text4,
73+
placeholder: "Placeholder")
74+
.autocapitalizationType(.none)
75+
.background(textBackgroundColor)
76+
}
6877
}
6978
}
7079
}

Sources/ResizingTextView/ResizingTextView.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public struct ResizingTextView: View, Equatable {
1818
#if os(macOS)
1919
var focusesNextKeyViewByTabKey: Bool = true
2020
var onInsertNewline: (() -> Bool)?
21+
#elseif os(iOS)
22+
var autocapitalizationType: UITextAutocapitalizationType = .sentences
2123
#endif
2224

2325
#if os(macOS)
@@ -133,8 +135,8 @@ public struct ResizingTextView: View, Equatable {
133135
lineLimit: lineLimit ?? .max,
134136
font: font,
135137
canHaveNewLineCharacters: canHaveNewLineCharacters,
136-
foregroundColor: Color(foregroundColor))
137-
138+
foregroundColor: Color(foregroundColor),
139+
autocapitalizationType: autocapitalizationType)
138140
if let placeholder {
139141
Text(placeholder)
140142
.font(Font(font))
@@ -162,6 +164,8 @@ public struct ResizingTextView: View, Equatable {
162164
&& lhs.isFocused == rhs.isFocused
163165
#if os(macOS)
164166
result = result && lhs.focusesNextKeyViewByTabKey == rhs.focusesNextKeyViewByTabKey
167+
#elseif os(iOS)
168+
result = result && lhs.autocapitalizationType == rhs.autocapitalizationType
165169
#endif
166170
return result
167171
}
@@ -205,5 +209,11 @@ public extension ResizingTextView {
205209
newSelf.font = font
206210
return newSelf
207211
}
212+
213+
func autocapitalizationType(_ autocapitalizationType: UITextAutocapitalizationType) -> Self {
214+
var newSelf = self
215+
newSelf.autocapitalizationType = autocapitalizationType
216+
return newSelf
217+
}
208218
#endif
209219
}

Sources/ResizingTextView/TextView (UIKit).swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ struct TextView: UIViewRepresentable {
1616
private var font: UIFont
1717
private var canHaveNewLineCharacters: Bool
1818
private var width: CGFloat?
19+
private var autocapitalizationType: UITextAutocapitalizationType
1920

2021
init(_ text: Binding<String>,
21-
isEditable: Bool = true,
22-
isScrollable: Bool = false,
23-
isSelectable: Bool = true,
24-
lineLimit: Int = 0,
22+
isEditable: Bool,
23+
isScrollable: Bool,
24+
isSelectable: Bool,
25+
lineLimit: Int,
2526
font: UIFont,
26-
canHaveNewLineCharacters: Bool = true,
27-
foregroundColor: Color = defaultForegroundColor) {
27+
canHaveNewLineCharacters: Bool,
28+
foregroundColor: Color,
29+
autocapitalizationType: UITextAutocapitalizationType) {
2830
self._text = text
2931
self.isEditable = isEditable
3032
self.isScrollable = isScrollable
@@ -33,14 +35,7 @@ struct TextView: UIViewRepresentable {
3335
self.foregroundColor = foregroundColor
3436
self.font = font
3537
self.canHaveNewLineCharacters = canHaveNewLineCharacters
36-
}
37-
38-
init(text: String) {
39-
self.init(
40-
Binding<String>.constant(text),
41-
isEditable: false,
42-
font: UIFont.preferredFont(forTextStyle: .body),
43-
foregroundColor: Self.defaultForegroundColor)
38+
self.autocapitalizationType = autocapitalizationType
4439
}
4540

4641
func makeUIView(context: Context) -> CustomTextView {
@@ -78,6 +73,9 @@ struct TextView: UIViewRepresentable {
7873
if view.textContainer.maximumNumberOfLines != lineLimit {
7974
view.textContainer.maximumNumberOfLines = lineLimit
8075
}
76+
if view.autocapitalizationType != autocapitalizationType {
77+
view.autocapitalizationType = autocapitalizationType
78+
}
8179
if lineLimit > 0 {
8280
if view.textContainer.lineBreakMode != .byTruncatingTail {
8381
view.textContainer.lineBreakMode = .byTruncatingTail

0 commit comments

Comments
 (0)