Skip to content

Commit 833c87b

Browse files
committed
Add text identifier
1 parent ff195cb commit 833c87b

5 files changed

Lines changed: 73 additions & 1 deletion

File tree

Example/SettingExample/PreferencesView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class PreferencesViewModel: ObservableObject {
2020
@AppStorage("notificationPromo") var notificationPromo = true
2121
@AppStorage("notificationUpdates") var notificationUpdates = true
2222
@AppStorage("color") var color = 0xFF3100
23+
@AppStorage("text") var text = ""
2324
@Published var showingAlert = false
2425
}
2526

@@ -229,6 +230,10 @@ struct PreferencesView: View {
229230
selectedIndex: $model.modeIndex
230231
)
231232
}
233+
234+
SettingGroup {
235+
SettingTextField(placeholder: "Enter text here", text: $model.text)
236+
}
232237

233238
SettingCustomView(id: "Custom Footer", titleForSearch: "Welcome to Setting!") {
234239
Text("Welcome to Setting!")

Sources/Setting.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public extension Setting {
4242
return nil
4343
case let picker as SettingPicker:
4444
return picker.title
45+
case let textField as SettingTextField:
46+
return textField.placeholder
4547
case let page as SettingPage:
4648
return page.title
4749
case let group as SettingGroup:
@@ -51,7 +53,7 @@ public extension Setting {
5153
case let customView as SettingCustomView:
5254
return customView.titleForSearch ?? "Custom"
5355
default:
54-
print("Nil! \(type(of: self))")
56+
print("Text identifier was nil for: \(type(of: self))")
5557
return nil
5658
}
5759
}
@@ -71,6 +73,8 @@ public extension Setting {
7173
return nil
7274
case let picker as SettingPicker:
7375
return picker.title
76+
case let textField as SettingTextField:
77+
return textField.placeholder
7478
case let page as SettingPage:
7579
return page.title
7680
case let group as SettingGroup:

Sources/Utilities/SettingIcon.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import SwiftUI
1313
*/
1414
public enum SettingIcon {
1515
case system(icon: String, foregroundColor: Color = .white, backgroundColor: Color)
16+
17+
/// Pass in a `foregroundColor` to render and recolor the image as a template.
1618
case image(name: String, inset: CGFloat, foregroundColor: Color?, backgroundColor: Color)
1719
case custom(view: AnyView)
1820
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// SettingTextField.swift
3+
// Setting
4+
//
5+
// Created by A. Zheng (github.com/aheze) on 2/24/23.
6+
// Copyright © 2023 A. Zheng. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
/**
12+
A text field.
13+
*/
14+
public struct SettingTextField: View, Setting {
15+
public var id: AnyHashable?
16+
public var placeholder: String
17+
@Binding public var text: String
18+
public var verticalPadding = CGFloat(14)
19+
public var horizontalPadding = CGFloat(16)
20+
21+
public init(
22+
id: AnyHashable? = nil,
23+
placeholder: String,
24+
text: Binding<String>,
25+
verticalPadding: CGFloat = CGFloat(14),
26+
horizontalPadding: CGFloat = CGFloat(16)
27+
) {
28+
self.id = id
29+
self.placeholder = placeholder
30+
self._text = text
31+
self.verticalPadding = verticalPadding
32+
self.horizontalPadding = horizontalPadding
33+
}
34+
35+
public var body: some View {
36+
SettingTextFieldView(
37+
placeholder: placeholder,
38+
text: $text,
39+
verticalPadding: verticalPadding,
40+
horizontalPadding: horizontalPadding
41+
)
42+
}
43+
}
44+
45+
struct SettingTextFieldView: View {
46+
let placeholder: String
47+
@Binding var text: String
48+
49+
var verticalPadding = CGFloat(14)
50+
var horizontalPadding = CGFloat(16)
51+
52+
var body: some View {
53+
TextField(placeholder, text: $text)
54+
.frame(maxWidth: .infinity, alignment: .leading)
55+
.padding(.vertical, verticalPadding)
56+
.padding(.horizontal, horizontalPadding)
57+
.accessibilityElement(children: .combine)
58+
}
59+
}

Sources/Views/SettingView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct SettingView: View {
2727
slider
2828
case let picker as SettingPicker:
2929
picker
30+
case let textField as SettingTextField:
31+
textField
3032
case let page as SettingPage:
3133

3234
if isPagePreview {

0 commit comments

Comments
 (0)