@@ -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