Skip to content

Commit 0074397

Browse files
authored
Add support for dynamic type (#9)
Enables dynamic type support and automatic size adjustments by default, but configurable on initialisation.
1 parent 81b5505 commit 0074397

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

Demo Project/ResponsiveTextFieldDemo.xcodeproj/project.pbxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@
210210
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
211211
GCC_WARN_UNUSED_FUNCTION = YES;
212212
GCC_WARN_UNUSED_VARIABLE = YES;
213-
IPHONEOS_DEPLOYMENT_TARGET = 14.4;
213+
IPHONEOS_DEPLOYMENT_TARGET = 14.5;
214214
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
215215
MTL_FAST_MATH = YES;
216216
ONLY_ACTIVE_ARCH = YES;
@@ -265,7 +265,7 @@
265265
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
266266
GCC_WARN_UNUSED_FUNCTION = YES;
267267
GCC_WARN_UNUSED_VARIABLE = YES;
268-
IPHONEOS_DEPLOYMENT_TARGET = 14.4;
268+
IPHONEOS_DEPLOYMENT_TARGET = 14.5;
269269
MTL_ENABLE_DEBUG_INFO = NO;
270270
MTL_FAST_MATH = YES;
271271
SDKROOT = iphoneos;

Demo Project/ResponsiveTextFieldDemo/ContentView.swift

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ struct ContentView: View {
7777
}
7878
)
7979
)
80+
.responsiveTextFieldFont(.preferredFont(forTextStyle: .title1))
8081
.responsiveKeyboardReturnType(.next)
8182
.responsiveTextFieldTextColor(.blue)
8283
.fixedSize(horizontal: false, vertical: true)

ResponsiveTextField.xcworkspace/contents.xcworkspacedata

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/ResponsiveTextField/ResponsiveTextField.swift

+30-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,21 @@ public struct ResponsiveTextField {
4848
var returnKeyType: UIReturnKeyType
4949

5050
/// Sets the text field font - use the `.responsiveKeyboardFont()` modifier.
51+
///
52+
/// - Note: if `adjustsFontForContentSizeCategory` is `true`, the font will only be set
53+
/// to this value once when the underlying text field is first created.
54+
///
5155
@Environment(\.textFieldFont)
5256
var font: UIFont
5357

58+
/// When `true`, configures the text field to automatically adjust its font based on the content size category.
59+
///
60+
/// - Note: When set to `true`, the underlying text field will not respond to changes to the `textFieldFont`
61+
/// environment variable. If you want to implement your own dynamic/state-driven font changes you should set this
62+
/// to `false` and handle font size adjustment manually.
63+
///
64+
var adjustsFontForContentSizeCategory: Bool
65+
5466
/// Sets the text field color - use the `.responsiveTextFieldColor()` modifier.
5567
@Environment(\.textFieldTextColor)
5668
var textColor: UIColor
@@ -110,6 +122,7 @@ public struct ResponsiveTextField {
110122
placeholder: String,
111123
text: Binding<String>,
112124
isSecure: Bool = false,
125+
adjustsFontForContentSizeCategory: Bool = true,
113126
firstResponderDemand: Binding<FirstResponderDemand?>? = nil,
114127
configuration: Configuration = .empty,
115128
onFirstResponderStateChanged: FirstResponderStateChangeHandler? = nil,
@@ -124,6 +137,7 @@ public struct ResponsiveTextField {
124137
self.firstResponderDemand = firstResponderDemand
125138
self.isSecure = isSecure
126139
self.configuration = configuration
140+
self.adjustsFontForContentSizeCategory = adjustsFontForContentSizeCategory
127141
self.onFirstResponderStateChanged = onFirstResponderStateChanged
128142
self.handleReturn = handleReturn
129143
self.handleDelete = handleDelete
@@ -220,7 +234,7 @@ public struct FirstResponderStateChangeHandler {
220234
/// safe to perform state changes that perform a view update inside this callback. However, programatic first
221235
/// responder state changes (where you change the demand state connected to the `firstResponderDemand`
222236
/// binding passed into `ResponsiveTextField`) happen as part of a view update - i.e. the demand change
223-
/// will trigger a view update and the `becomeFirstResponder()` call will happn in the `updateUIView`
237+
/// will trigger a view update and the `becomeFirstResponder()` call will happen in the `updateUIView`
224238
/// as part of that view change event.
225239
///
226240
/// This means that the change handler callback will be called as part of the view update and if that change handler
@@ -292,6 +306,7 @@ extension ResponsiveTextField: UIViewRepresentable {
292306
textField.isEnabled = isEnabled
293307
textField.isSecureTextEntry = isSecure
294308
textField.font = font
309+
textField.adjustsFontForContentSizeCategory = adjustsFontForContentSizeCategory
295310
textField.textColor = textColor
296311
textField.textAlignment = textAlignment
297312
textField.returnKeyType = returnKeyType
@@ -318,9 +333,15 @@ extension ResponsiveTextField: UIViewRepresentable {
318333
uiView.isEnabled = isEnabled
319334
uiView.isSecureTextEntry = isSecure
320335
uiView.returnKeyType = returnKeyType
321-
uiView.font = font
322336
uiView.text = text.wrappedValue
323337

338+
if !adjustsFontForContentSizeCategory {
339+
// We should only support dynamic font changes using our own environment
340+
// value if dynamic type support is disabled otherwise we will override
341+
// the automatically adjusted font.
342+
uiView.font = font
343+
}
344+
324345
switch (uiView.isFirstResponder, firstResponderDemand?.wrappedValue) {
325346
case (true, .shouldResignFirstResponder):
326347
uiView.resignFirstResponder()
@@ -557,6 +578,13 @@ struct ResponsiveTextField_Previews: PreviewProvider {
557578
.previewLayout(.sizeThatFits)
558579
.previewDisplayName("Text Styling")
559580

581+
TextFieldPreview(configuration: .email, text: "[email protected]")
582+
.responsiveTextFieldFont(.preferredFont(forTextStyle: .body))
583+
.responsiveTextFieldTextColor(.systemBlue)
584+
.previewLayout(.sizeThatFits)
585+
.environment(\.sizeCategory, .extraExtraExtraLarge)
586+
.previewDisplayName("Dynamic Font Size")
587+
560588
TextFieldPreview(configuration: .empty, text: "This is some text")
561589
.responsiveTextFieldTextAlignment(.center)
562590
.previewLayout(.sizeThatFits)

0 commit comments

Comments
 (0)