@@ -13,7 +13,7 @@ import CombineSchedulers
13
13
///
14
14
public struct ResponsiveTextField {
15
15
/// The text field placeholder string.
16
- let placeholder : String
16
+ let placeholder : String ?
17
17
18
18
/// A binding to the text state that will hold the typed text
19
19
let text : Binding < String >
@@ -56,6 +56,10 @@ public struct ResponsiveTextField {
56
56
@Environment ( \. textFieldFont)
57
57
var font : UIFont
58
58
59
+ /// Sets the text field placeholder color - use the `.responsiveTextFieldPlaceholderColor()` modifier.
60
+ @Environment ( \. textFieldPlaceholderColor)
61
+ var placeholderColor : UIColor
62
+
59
63
/// When `true`, configures the text field to automatically adjust its font based on the content size category.
60
64
///
61
65
/// - Note: When set to `true`, the underlying text field will not respond to changes to the `textFieldFont`
@@ -123,7 +127,7 @@ public struct ResponsiveTextField {
123
127
var standardEditActionHandler : StandardEditActionHandling < UITextField > ?
124
128
125
129
public init (
126
- placeholder: String ,
130
+ placeholder: String ? ,
127
131
text: Binding < String > ,
128
132
isSecure: Bool = false ,
129
133
adjustsFontForContentSizeCategory: Bool = true ,
@@ -330,6 +334,14 @@ extension ResponsiveTextField: UIViewRepresentable {
330
334
textField. textColor = textColor
331
335
textField. textAlignment = textAlignment
332
336
textField. returnKeyType = returnKeyType
337
+
338
+ if let placeholder {
339
+ textField. attributedPlaceholder = NSAttributedString (
340
+ string: placeholder,
341
+ attributes: [ NSAttributedString . Key. foregroundColor: self . placeholderColor]
342
+ )
343
+ }
344
+
333
345
textField. delegate = context. coordinator
334
346
textField. addTarget ( context. coordinator,
335
347
action: #selector( Coordinator . textFieldTextChanged ( _: ) ) ,
@@ -357,6 +369,13 @@ extension ResponsiveTextField: UIViewRepresentable {
357
369
uiView. textColor = textColor
358
370
uiView. textAlignment = textAlignment
359
371
372
+ if let placeholder {
373
+ uiView. attributedPlaceholder = NSAttributedString (
374
+ string: placeholder,
375
+ attributes: [ NSAttributedString . Key. foregroundColor: self . placeholderColor]
376
+ )
377
+ }
378
+
360
379
if !adjustsFontForContentSizeCategory {
361
380
// We should only support dynamic font changes using our own environment
362
381
// value if dynamic type support is disabled otherwise we will override
@@ -553,6 +572,11 @@ public extension View {
553
572
environment ( \. textFieldTextColor, color)
554
573
}
555
574
575
+ /// Sets the text field placeholder text color on any child `ResponsiveTextField` views.
576
+ func responsiveTextFieldPlaceholderColor( _ color: UIColor ) -> some View {
577
+ environment ( \. textFieldPlaceholderColor, color)
578
+ }
579
+
556
580
/// Sets the text field text alignment on any child `ResponsiveTextField` views.
557
581
func responsiveTextFieldTextAlignment( _ alignment: NSTextAlignment ) -> some View {
558
582
environment ( \. textFieldTextAlignment, alignment)
@@ -603,6 +627,7 @@ struct ResponsiveTextField_Previews: PreviewProvider {
603
627
TextFieldPreview ( configuration
: . email
, text
: " [email protected] " )
604
628
. responsiveTextFieldFont ( . preferredFont( forTextStyle: . body) )
605
629
. responsiveTextFieldTextColor ( . systemBlue)
630
+ . responsiveTextFieldPlaceholderColor ( . gray)
606
631
. previewLayout ( . sizeThatFits)
607
632
. environment ( \. sizeCategory, . extraExtraExtraLarge)
608
633
. previewDisplayName ( " Dynamic Font Size " )
0 commit comments