This repository has been archived by the owner on May 1, 2024. It is now read-only.
/
MaterialTextManager.cs
107 lines (90 loc) · 4.29 KB
/
MaterialTextManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using CoreGraphics;
using MaterialComponents;
using UIKit;
using MTextField = MaterialComponents.TextField;
using MTextInputControllerFilled = MaterialComponents.TextInputControllerFilled;
using MTextInputControllerBase = MaterialComponents.TextInputControllerBase;
using System.Collections.Generic;
using ObjCRuntime;
using Foundation;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.iOS;
namespace Xamarin.Forms.Material.iOS
{
internal static class MaterialTextManager
{
public static void Init(IMaterialEntryRenderer element, IMaterialTextField textField, IFontElement fontElement)
{
var containerScheme = textField.ContainerScheme;
textField.TextInput.ClearButtonMode = UITextFieldViewMode.Never;
textField.ActiveTextInputController = new MTextInputControllerFilled(textField.TextInput);
textField.TextInput.TextInsetsMode = TextInputTextInsetsMode.IfContent;
containerScheme.TypographyScheme = CreateTypographyScheme();
containerScheme.ColorScheme = (SemanticColorScheme)CreateColorScheme();
ApplyTypographyScheme(textField, fontElement);
ApplyTheme(textField, element);
}
public static void ApplyTypographyScheme(IMaterialTextField textField, IFontElement fontElement)
{
var containerScheme = textField.ContainerScheme;
textField.TextInput.Font = fontElement?.ToUIFont();
containerScheme.TypographyScheme.Subtitle1 = textField.TextInput.Font;
ApplyContainerTheme(textField);
}
static void ApplyContainerTheme(IMaterialTextField textField)
{
var containerScheme = textField.ContainerScheme;
if (textField.ActiveTextInputController is MTextInputControllerFilled filled)
filled.ApplyTheme(containerScheme);
}
public static void ApplyTheme(IMaterialTextField textField, IMaterialEntryRenderer element)
{
if (element == null)
return;
if (textField.ActiveTextInputController == null)
return;
textField.ContainerScheme.ColorScheme = (SemanticColorScheme)CreateColorScheme();
ApplyContainerTheme(textField);
var textColor = MaterialColors.GetEntryTextColor(element.TextColor);
var placeHolderColors = MaterialColors.GetPlaceHolderColor(element.PlaceholderColor, element.TextColor);
var underlineColors = MaterialColors.GetUnderlineColor(element.PlaceholderColor);
textField.TextInput.TextColor = textColor;
textField.ActiveTextInputController.InlinePlaceholderColor = placeHolderColors.InlineColor;
textField.ActiveTextInputController.FloatingPlaceholderNormalColor = placeHolderColors.InlineColor;
textField.ActiveTextInputController.FloatingPlaceholderActiveColor = placeHolderColors.FloatingColor;
// BackgroundColor
textField.ActiveTextInputController.BorderFillColor = MaterialColors.CreateEntryFilledInputBackgroundColor(element.BackgroundColor, element.TextColor);
textField.ActiveTextInputController.ActiveColor = underlineColors.FocusedColor;
textField.ActiveTextInputController.NormalColor = underlineColors.UnFocusedColor;
}
public static void UpdatePlaceholder(IMaterialTextField textField, IMaterialEntryRenderer element)
{
var placeholderText = element.Placeholder ?? String.Empty;
textField.ActiveTextInputController.PlaceholderText = placeholderText;
ApplyTheme(textField, element);
var previous = textField.ActiveTextInputController.FloatingPlaceholderScale;
if (String.IsNullOrWhiteSpace(placeholderText))
textField.ActiveTextInputController.FloatingPlaceholderScale = 0;
else
textField.ActiveTextInputController.FloatingPlaceholderScale = (float)TextInputControllerBase.FloatingPlaceholderScaleDefault;
if (previous != textField.ActiveTextInputController.FloatingPlaceholderScale && element is IVisualElementRenderer controller)
controller.Element?.InvalidateMeasureInternal(InvalidationTrigger.VerticalOptionsChanged);
}
public static void UpdateTextColor(IMaterialTextField textField, IMaterialEntryRenderer element)
{
var uIColor = MaterialColors.GetEntryTextColor(element.TextColor);
textField.ContainerScheme.ColorScheme.OnSurfaceColor = uIColor;
textField.ContainerScheme.ColorScheme.PrimaryColor = uIColor;
}
static IColorScheming CreateColorScheme()
{
var returnValue = MaterialColors.Light.CreateColorScheme();
return returnValue;
}
static TypographyScheme CreateTypographyScheme()
{
return new TypographyScheme();
}
}
}