/
MaterialMultiLineTextField.cs
105 lines (88 loc) · 2.85 KB
/
MaterialMultiLineTextField.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
using System;
using CoreGraphics;
using MaterialComponents;
using UIKit;
using MMultilineTextField = MaterialComponents.MultilineTextField;
using MTextInputControllerBase = MaterialComponents.TextInputControllerBase;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Material.iOS
{
public class MaterialMultilineTextField : MMultilineTextField, IMaterialTextField
{
CGSize _contentSize;
public MaterialMultilineTextField(IMaterialEntryRenderer element, IFontElement fontElement)
{
ContainerScheme = new ContainerScheme();
MaterialTextManager.Init(element, this, fontElement);
}
public ContainerScheme ContainerScheme { get; }
public SemanticColorScheme ColorScheme { get; set; }
public TypographyScheme TypographyScheme { get; set; }
public MTextInputControllerBase ActiveTextInputController { get; set; }
public ITextInput TextInput => this;
public override CGRect Frame
{
get => base.Frame;
set
{
base.Frame = value;
UpdateIfTextViewShouldStopExpanding();
}
}
public override CGSize SizeThatFits(CGSize size)
{
bool expandTurnedBackOn = UpdateIfTextViewShouldCollapse();
var result = base.SizeThatFits(size);
if (nfloat.IsInfinity(result.Width))
result = SystemLayoutSizeFittingSize(result, (float)UILayoutPriority.FittingSizeLevel, (float)UILayoutPriority.DefaultHigh);
if (ExpandsOnOverflow)
_contentSize = result;
else
_contentSize = TextView.ContentSize;
if (!expandTurnedBackOn)
UpdateIfTextViewShouldStopExpanding();
return result;
}
bool UpdateIfTextViewShouldCollapse()
{
if (!ExpandsOnOverflow &&
!AutoSizeWithChanges &&
!ShouldRestrainSize())
{
ExpandsOnOverflow = true;
return true;
}
return false;
}
bool ShouldRestrainSize()
{
if (TextView?.Font == null)
return false;
return (((NumberOfLines + 1) * TextView.Font.LineHeight) > Frame.Height);
}
void UpdateIfTextViewShouldStopExpanding()
{
if (!UpdateIfTextViewShouldCollapse() &&
!AutoSizeWithChanges &&
ExpandsOnOverflow &&
ShouldRestrainSize())
{
ExpandsOnOverflow = false;
}
}
int NumberOfLines
{
get
{
if (TextView?.ContentSize == null || TextView.Font == null || TextView.Font.LineHeight == 0)
return 0;
return (int)(_contentSize.Height / TextView.Font.LineHeight);
}
}
internal bool AutoSizeWithChanges { get; set; } = false;
internal void ApplyTypographyScheme(IFontElement fontElement) => MaterialTextManager.ApplyTypographyScheme(this, fontElement);
internal void ApplyTheme(IMaterialEntryRenderer element) => MaterialTextManager.ApplyTheme(this, element);
internal void UpdatePlaceholder(IMaterialEntryRenderer element) => MaterialTextManager.UpdatePlaceholder(this, element);
internal void UpdateTextColor(IMaterialEntryRenderer element) => MaterialTextManager.UpdateTextColor(this, element);
}
}