@@ -10,6 +10,8 @@ namespace Xamarin.Forms.Material.iOS
1010{
1111 internal static class MaterialTextManager
1212 {
13+ static double AlphaAdjustment = 0.0 ;
14+
1315 public static void Init ( IMaterialEntryRenderer element , IMaterialTextField textField , IFontElement fontElement )
1416 {
1517 var containerScheme = textField . ContainerScheme ;
@@ -49,8 +51,10 @@ public static void ApplyTheme(IMaterialTextField textField, IMaterialEntryRender
4951 textField . ContainerScheme . ColorScheme = ( SemanticColorScheme ) CreateColorScheme ( ) ;
5052 ApplyContainerTheme ( textField ) ;
5153
52- var textColor = MaterialColors . GetEntryTextColor ( element . TextColor ) ;
53- var placeHolderColors = MaterialColors . GetPlaceHolderColor ( element . PlaceholderColor , element . TextColor ) ;
54+ var adjustedTextColor = AdjustTextColor ( element ) ;
55+
56+ var textColor = MaterialColors . GetEntryTextColor ( adjustedTextColor ) ;
57+ var placeHolderColors = MaterialColors . GetPlaceHolderColor ( element . PlaceholderColor , adjustedTextColor ) ;
5458 var underlineColors = MaterialColors . GetUnderlineColor ( element . PlaceholderColor ) ;
5559
5660 textField . TextInput . TextColor = textColor ;
@@ -63,7 +67,7 @@ public static void ApplyTheme(IMaterialTextField textField, IMaterialEntryRender
6367 if ( Brush . IsNullOrEmpty ( brush ) )
6468 {
6569 // BackgroundColor
66- textField . ActiveTextInputController . BorderFillColor = MaterialColors . CreateEntryFilledInputBackgroundColor ( element . BackgroundColor , element . TextColor ) ;
70+ textField . ActiveTextInputController . BorderFillColor = MaterialColors . CreateEntryFilledInputBackgroundColor ( element . BackgroundColor , adjustedTextColor ) ;
6771 }
6872 else
6973 {
@@ -114,11 +118,45 @@ public static void UpdatePlaceholder(IMaterialTextField textField, IMaterialEntr
114118
115119 public static void UpdateTextColor ( IMaterialTextField textField , IMaterialEntryRenderer element )
116120 {
117- var uIColor = MaterialColors . GetEntryTextColor ( element . TextColor ) ;
121+ var adjustedTextColor = AdjustTextColor ( element ) ;
122+
123+ var uIColor = MaterialColors . GetEntryTextColor ( adjustedTextColor ) ;
118124 textField . ContainerScheme . ColorScheme . OnSurfaceColor = uIColor ;
119125 textField . ContainerScheme . ColorScheme . PrimaryColor = uIColor ;
120126 }
121127
128+ static Color AdjustTextColor ( IMaterialEntryRenderer element )
129+ {
130+ if ( Forms . IsiOS14OrNewer )
131+ {
132+ // This is a workaround for an iOS/Material bug; https://github.com/xamarin/Xamarin.Forms/issues/12246
133+ // If we are on iOS 14, and we have multiple material text entry fields of the same color,
134+ // and any of them are password fields, setting them to the same TextColor value will cause the application
135+ // to hang when a password field loses focus.
136+
137+ // So to work around this, we make an imperceptible adjustment to the alpha value of the color each time
138+ // we set it; that way, none of the text entry fields have _exactly_ the same color and we avoid the bug
139+
140+ // Obviously this will start to become noticeable after the first 20 million or so text entry fields are displayed.
141+ // We apologize for the inconvenience.
142+
143+ var elementTextColor = element . TextColor ;
144+ AlphaAdjustment += 0.0000001 ;
145+
146+ var adjustedAlpha = elementTextColor . A - AlphaAdjustment ;
147+ if ( adjustedAlpha < 0 )
148+ {
149+ // Below an alpha of 0.01 stuff on iOS doesn't show up in hit tests anyway, so it seems unlikely
150+ // that the entry will get focus and cause the issue.
151+ adjustedAlpha = 0 ;
152+ }
153+
154+ return new Color ( elementTextColor . R , elementTextColor . G , elementTextColor . B , adjustedAlpha ) ;
155+ }
156+
157+ return element . TextColor ;
158+ }
159+
122160 static IColorScheming CreateColorScheme ( )
123161 {
124162 var returnValue = MaterialColors . Light . CreateColorScheme ( ) ;
0 commit comments