@@ -48,9 +48,21 @@ public struct ResponsiveTextField {
48
48
var returnKeyType : UIReturnKeyType
49
49
50
50
/// 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
+ ///
51
55
@Environment ( \. textFieldFont)
52
56
var font : UIFont
53
57
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
+
54
66
/// Sets the text field color - use the `.responsiveTextFieldColor()` modifier.
55
67
@Environment ( \. textFieldTextColor)
56
68
var textColor : UIColor
@@ -110,6 +122,7 @@ public struct ResponsiveTextField {
110
122
placeholder: String ,
111
123
text: Binding < String > ,
112
124
isSecure: Bool = false ,
125
+ adjustsFontForContentSizeCategory: Bool = true ,
113
126
firstResponderDemand: Binding < FirstResponderDemand ? > ? = nil ,
114
127
configuration: Configuration = . empty,
115
128
onFirstResponderStateChanged: FirstResponderStateChangeHandler ? = nil ,
@@ -124,6 +137,7 @@ public struct ResponsiveTextField {
124
137
self . firstResponderDemand = firstResponderDemand
125
138
self . isSecure = isSecure
126
139
self . configuration = configuration
140
+ self . adjustsFontForContentSizeCategory = adjustsFontForContentSizeCategory
127
141
self . onFirstResponderStateChanged = onFirstResponderStateChanged
128
142
self . handleReturn = handleReturn
129
143
self . handleDelete = handleDelete
@@ -220,7 +234,7 @@ public struct FirstResponderStateChangeHandler {
220
234
/// safe to perform state changes that perform a view update inside this callback. However, programatic first
221
235
/// responder state changes (where you change the demand state connected to the `firstResponderDemand`
222
236
/// 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`
224
238
/// as part of that view change event.
225
239
///
226
240
/// 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 {
292
306
textField. isEnabled = isEnabled
293
307
textField. isSecureTextEntry = isSecure
294
308
textField. font = font
309
+ textField. adjustsFontForContentSizeCategory = adjustsFontForContentSizeCategory
295
310
textField. textColor = textColor
296
311
textField. textAlignment = textAlignment
297
312
textField. returnKeyType = returnKeyType
@@ -318,9 +333,15 @@ extension ResponsiveTextField: UIViewRepresentable {
318
333
uiView. isEnabled = isEnabled
319
334
uiView. isSecureTextEntry = isSecure
320
335
uiView. returnKeyType = returnKeyType
321
- uiView. font = font
322
336
uiView. text = text. wrappedValue
323
337
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
+
324
345
switch ( uiView. isFirstResponder, firstResponderDemand? . wrappedValue) {
325
346
case ( true , . shouldResignFirstResponder) :
326
347
uiView. resignFirstResponder ( )
@@ -557,6 +578,13 @@ struct ResponsiveTextField_Previews: PreviewProvider {
557
578
. previewLayout ( . sizeThatFits)
558
579
. previewDisplayName ( " Text Styling " )
559
580
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
+
560
588
TextFieldPreview ( configuration: . empty, text: " This is some text " )
561
589
. responsiveTextFieldTextAlignment ( . center)
562
590
. previewLayout ( . sizeThatFits)
0 commit comments