summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Label.cs
blob: 0634e080a83786c828fcf8b5bbeae6a2bcb9a623 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.ComponentModel;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform;

namespace Xamarin.Forms
{
	[ContentProperty("Text")]
	[RenderWith(typeof(_LabelRenderer))]
	public class Label : View, IFontElement, IElementConfiguration<Label>
	{
		public static readonly BindableProperty HorizontalTextAlignmentProperty = BindableProperty.Create("HorizontalTextAlignment", typeof(TextAlignment), typeof(Label), TextAlignment.Start,
			propertyChanged: OnHorizontalTextAlignmentPropertyChanged);

		[Obsolete("XAlignProperty is obsolete. Please use HorizontalTextAlignmentProperty instead.")]
		public static readonly BindableProperty XAlignProperty = HorizontalTextAlignmentProperty;

		public static readonly BindableProperty VerticalTextAlignmentProperty = BindableProperty.Create("VerticalTextAlignment", typeof(TextAlignment), typeof(Label), TextAlignment.Start,
			propertyChanged: OnVerticalTextAlignmentPropertyChanged);

		[Obsolete("YAlignProperty is obsolete. Please use VerticalTextAlignmentProperty instead.")]
		public static readonly BindableProperty YAlignProperty = VerticalTextAlignmentProperty;

		public static readonly BindableProperty TextColorProperty = BindableProperty.Create("TextColor", typeof(Color), typeof(Label), Color.Default);

		public static readonly BindableProperty FontProperty = BindableProperty.Create("Font", typeof(Font), typeof(Label), default(Font), propertyChanged: FontStructPropertyChanged);

		public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(Label), default(string), propertyChanged: OnTextPropertyChanged);

		public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create("FontFamily", typeof(string), typeof(Label), default(string), propertyChanged: OnFontFamilyChanged);

		public static readonly BindableProperty FontSizeProperty = BindableProperty.Create("FontSize", typeof(double), typeof(Label), -1.0, propertyChanged: OnFontSizeChanged,
			defaultValueCreator: bindable => Device.GetNamedSize(NamedSize.Default, (Label)bindable));

		public static readonly BindableProperty FontAttributesProperty = BindableProperty.Create("FontAttributes", typeof(FontAttributes), typeof(Label), FontAttributes.None,
			propertyChanged: OnFontAttributesChanged);

		public static readonly BindableProperty FormattedTextProperty = BindableProperty.Create("FormattedText", typeof(FormattedString), typeof(Label), default(FormattedString),
			propertyChanging: (bindable, oldvalue, newvalue) =>
			{
				if (oldvalue != null)
					((FormattedString)oldvalue).PropertyChanged -= ((Label)bindable).OnFormattedTextChanged;
			}, propertyChanged: (bindable, oldvalue, newvalue) =>
			{
				if (newvalue != null)
					((FormattedString)newvalue).PropertyChanged += ((Label)bindable).OnFormattedTextChanged;
				((Label)bindable).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
				if (newvalue != null)
					((Label)bindable).Text = null;
			});

		public static readonly BindableProperty LineBreakModeProperty = BindableProperty.Create("LineBreakMode", typeof(LineBreakMode), typeof(Label), LineBreakMode.WordWrap,
			propertyChanged: (bindable, oldvalue, newvalue) => ((Label)bindable).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged));

		readonly Lazy<PlatformConfigurationRegistry<Label>> _platformConfigurationRegistry;

		public Label()
		{
			_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<Label>>(() => new PlatformConfigurationRegistry<Label>(this));
		}

		bool _cancelEvents;

		[Obsolete("Please use the Font attributes which are on the class itself. Obsoleted in v1.3.0")]
		public Font Font
		{
			get { return (Font)GetValue(FontProperty); }
			set { SetValue(FontProperty, value); }
		}

		public FormattedString FormattedText
		{
			get { return (FormattedString)GetValue(FormattedTextProperty); }
			set { SetValue(FormattedTextProperty, value); }
		}

		public TextAlignment HorizontalTextAlignment
		{
			get { return (TextAlignment)GetValue(HorizontalTextAlignmentProperty); }
			set { SetValue(HorizontalTextAlignmentProperty, value); }
		}

		public LineBreakMode LineBreakMode
		{
			get { return (LineBreakMode)GetValue(LineBreakModeProperty); }
			set { SetValue(LineBreakModeProperty, value); }
		}

		public string Text
		{
			get { return (string)GetValue(TextProperty); }
			set { SetValue(TextProperty, value); }
		}

		public Color TextColor
		{
			get { return (Color)GetValue(TextColorProperty); }
			set { SetValue(TextColorProperty, value); }
		}

		public TextAlignment VerticalTextAlignment
		{
			get { return (TextAlignment)GetValue(VerticalTextAlignmentProperty); }
			set { SetValue(VerticalTextAlignmentProperty, value); }
		}

		[Obsolete("XAlign is obsolete. Please use HorizontalTextAlignment instead.")]
		public TextAlignment XAlign
		{
			get { return (TextAlignment)GetValue(XAlignProperty); }
			set { SetValue(XAlignProperty, value); }
		}

		[Obsolete("YAlign is obsolete. Please use VerticalTextAlignment instead.")]
		public TextAlignment YAlign
		{
			get { return (TextAlignment)GetValue(YAlignProperty); }
			set { SetValue(YAlignProperty, value); }
		}

		public FontAttributes FontAttributes
		{
			get { return (FontAttributes)GetValue(FontAttributesProperty); }
			set { SetValue(FontAttributesProperty, value); }
		}

		public string FontFamily
		{
			get { return (string)GetValue(FontFamilyProperty); }
			set { SetValue(FontFamilyProperty, value); }
		}

		[TypeConverter(typeof(FontSizeConverter))]
		public double FontSize
		{
			get { return (double)GetValue(FontSizeProperty); }
			set { SetValue(FontSizeProperty, value); }
		}

		static void FontStructPropertyChanged(BindableObject bindable, object oldValue, object newValue)
		{
			var label = (Label)bindable;
			if (label._cancelEvents)
				return;

			label._cancelEvents = true;

			var font = (Font)newValue;
			if (font == Font.Default)
			{
				label.FontFamily = null;
				label.FontSize = Device.GetNamedSize(NamedSize.Default, label);
				label.FontAttributes = FontAttributes.None;
			}
			else
			{
				label.FontFamily = font.FontFamily;
				if (font.UseNamedSize)
				{
					label.FontSize = Device.GetNamedSize(font.NamedSize, label.GetType(), true);
				}
				else
				{
					label.FontSize = font.FontSize;
				}
				label.FontAttributes = font.FontAttributes;
			}

			label._cancelEvents = false;

			label.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
		}

		static void OnFontAttributesChanged(BindableObject bindable, object oldValue, object newValue)
		{
			var label = (Label)bindable;

			if (label._cancelEvents)
				return;

			label._cancelEvents = true;

			var attributes = (FontAttributes)newValue;

			object[] values = label.GetValues(FontFamilyProperty, FontSizeProperty);
			var family = (string)values[0];
			if (family != null)
			{
#pragma warning disable 0618 // retain until Font removed
				label.Font = Font.OfSize(family, (double)values[1]).WithAttributes(attributes);
#pragma warning restore 0618
			}
			else
			{
#pragma warning disable 0618 // retain until Font removed
				label.Font = Font.SystemFontOfSize((double)values[1], attributes);
#pragma warning restore 0618
			}

			label._cancelEvents = false;

			label.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
		}

		static void OnFontFamilyChanged(BindableObject bindable, object oldValue, object newValue)
		{
			var label = (Label)bindable;
			if (label._cancelEvents)
				return;

			label._cancelEvents = true;

			object[] values = label.GetValues(FontSizeProperty, FontAttributesProperty);

			var family = (string)newValue;
			if (family != null)
			{
#pragma warning disable 0618 // retain until Font removed
				label.Font = Font.OfSize(family, (double)values[0]).WithAttributes((FontAttributes)values[1]);
#pragma warning restore 0618
			}
			else
			{
#pragma warning disable 0618 // retain until Font removed
				label.Font = Font.SystemFontOfSize((double)values[0], (FontAttributes)values[1]);
#pragma warning restore 0618
			}

			label._cancelEvents = false;
			label.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
		}

		static void OnFontSizeChanged(BindableObject bindable, object oldValue, object newValue)
		{
			var label = (Label)bindable;
			if (label._cancelEvents)
				return;

			label._cancelEvents = true;

			object[] values = label.GetValues(FontFamilyProperty, FontAttributesProperty);

			var size = (double)newValue;
			var family = (string)values[0];
			if (family != null)
			{
#pragma warning disable 0618 // retain until Font removed
				label.Font = Font.OfSize(family, size).WithAttributes((FontAttributes)values[1]);
#pragma warning restore 0618
			}
			else
			{
#pragma warning disable 0618 // retain until Font removed
				label.Font = Font.SystemFontOfSize(size, (FontAttributes)values[1]);
#pragma warning restore 0618
			}

			label._cancelEvents = false;

			label.InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
		}

		void OnFormattedTextChanged(object sender, PropertyChangedEventArgs e)
		{
			InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
			OnPropertyChanged("FormattedText");
		}

		static void OnHorizontalTextAlignmentPropertyChanged(BindableObject bindable, object oldValue, object newValue)
		{
			var label = (Label)bindable;
#pragma warning disable 0618 // retain until XAlign removed
			label.OnPropertyChanged(nameof(XAlign));
#pragma warning restore 0618
		}

		static void OnTextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
		{
			var label = (Label)bindable;
			LineBreakMode breakMode = label.LineBreakMode;
			bool isVerticallyFixed = (label.Constraint & LayoutConstraint.VerticallyFixed) != 0;
			bool isSingleLine = !(breakMode == LineBreakMode.CharacterWrap || breakMode == LineBreakMode.WordWrap);
			if (!isVerticallyFixed || !isSingleLine)
				((Label)bindable).InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
			if (newvalue != null)
				((Label)bindable).FormattedText = null;
		}

		static void OnVerticalTextAlignmentPropertyChanged(BindableObject bindable, object oldValue, object newValue)
		{
			var label = (Label)bindable;
#pragma warning disable 0618 // retain until YAlign removed
			label.OnPropertyChanged(nameof(YAlign));
#pragma warning restore 0618
		}

		public IPlatformElementConfiguration<T, Label> On<T>() where T : IConfigPlatform
		{
			return _platformConfigurationRegistry.Value.On<T>();
		}
	}
}