summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Native/Label.cs
blob: 8d095363be4e4e57515c4e6eb635f36c37e8ea71 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System;
using ElmSharp;
using ELabel = ElmSharp.Label;
using EColor = ElmSharp.Color;
using ESize = ElmSharp.Size;

namespace Xamarin.Forms.Platform.Tizen.Native
{
	/// <summary>
	/// The Label class extends <c>ElmSharp.Label</c> to be better suited for Xamarin renderers.
	/// Mainly the formatted text support.
	/// </summary>
	public class Label : ELabel, ITextable, IMeasurable
	{
		/// <summary>
		/// The _span holds the content of the label.
		/// </summary>
		readonly Span _span = new Span();

		/// <summary>
		/// Initializes a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.Native.Label"/> class.
		/// </summary>
		/// <param name="parent">Parent evas object.</param>
		public Label(EvasObject parent) : base(parent)
		{
		}

		/// <summary>
		/// Get or sets the formatted text.
		/// </summary>
		/// <remarks>Setting <c>FormattedText</c> changes the value of the <c>Text</c> property.</remarks>
		/// <value>The formatted text.</value>
		public FormattedString FormattedText
		{
			get
			{
				return _span.FormattedText;
			}

			set
			{
				if (value != _span.FormattedText)
				{
					_span.FormattedText = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the text.
		/// </summary>
		/// <remarks>Setting <c>Text</c> overwrites the value of the <c>FormattedText</c> property too.</remarks>
		/// <value>The content of the label.</value>
		public override string Text
		{
			get
			{
				return _span.Text;
			}

			set
			{
				if (value != _span.Text)
				{
					_span.Text = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the color of the formatted text.
		/// </summary>
		/// <value>The color of the text.</value>
		public EColor TextColor
		{
			get
			{
				return _span.ForegroundColor;
			}

			set
			{
				if (!_span.ForegroundColor.Equals(value))
				{
					_span.ForegroundColor = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the background color for the text.
		/// </summary>
		/// <value>The color of the label's background.</value>
		public EColor TextBackgroundColor
		{
			get
			{
				return _span.BackgroundColor;
			}

			set
			{
				if (!_span.BackgroundColor.Equals(value))
				{
					_span.BackgroundColor = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the font family for the text.
		/// </summary>
		/// <value>The font family.</value>
		public string FontFamily
		{
			get
			{
				return _span.FontFamily;
			}

			set
			{
				if (value != _span.FontFamily)
				{
					_span.FontFamily = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the font attributes.
		/// </summary>
		/// <value>The font attributes.</value>
		public FontAttributes FontAttributes
		{
			get
			{
				return _span.FontAttributes;
			}

			set
			{
				if (value != _span.FontAttributes)
				{
					_span.FontAttributes = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the font size for the text.
		/// </summary>
		/// <value>The size of the font.</value>
		public double FontSize
		{
			get
			{
				return _span.FontSize;
			}

			set
			{
				if (value != _span.FontSize)
				{
					_span.FontSize = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the font weight for the text.
		/// </summary>
		/// <value>The weight of the font.</value>
		public string FontWeight
		{
			get
			{
				return _span.FontWeight;
			}

			set
			{
				if (value != _span.FontWeight)
				{
					_span.FontWeight = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the line wrap option.
		/// </summary>
		/// <value>The line break mode.</value>
		public LineBreakMode LineBreakMode
		{
			get
			{
				return _span.LineBreakMode;
			}

			set
			{
				if (value != _span.LineBreakMode)
				{
					var previousMode = _span.LineBreakMode;
					_span.LineBreakMode = value;
					switch(value)
					{
						case LineBreakMode.NoWrap:
							LineWrapType = WrapType.None;
							IsEllipsis = false;
							break;
						case LineBreakMode.CharacterWrap:
							LineWrapType = WrapType.Char;
							IsEllipsis = false;
							break;
						case LineBreakMode.WordWrap:
							LineWrapType = WrapType.Word;
							IsEllipsis = false;
							break;
						case LineBreakMode.MixedWrap:
							LineWrapType = WrapType.Mixed;
							IsEllipsis = false;
							break;
						default:
							LineWrapType = WrapType.None;
							IsEllipsis = true;
							break;
					}
					if (IsEllipsis || previousMode >= LineBreakMode.HeadTruncation)
					{
						ApplyTextAndStyle();
					}
				}
			}
		}

		/// <summary>
		/// Gets or sets the horizontal text alignment.
		/// </summary>
		/// <value>The horizontal text alignment.</value>
		public TextAlignment HorizontalTextAlignment
		{
			get
			{
				return _span.HorizontalTextAlignment;
			}

			set
			{
				if (value != _span.HorizontalTextAlignment)
				{
					_span.HorizontalTextAlignment = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the vertical text alignment.
		/// </summary>
		/// <value>The vertical text alignment.</value>
		public TextAlignment VerticalTextAlignment
		{
			// README: It only work on Tizen 4.0
			get
			{
				double valign = GetVerticalTextAlignment("elm.text");
				if (valign == 0.0)
				{
					return TextAlignment.Start;
				}
				else if (valign == 0.5)
				{
					return TextAlignment.Center;
				}
				else if (valign == 1.0)
				{
					return TextAlignment.End;
				}
				else
				{
					return TextAlignment.Auto;
				}
			}
			set
			{
				double valign = 0;
				switch (value)
				{
					case TextAlignment.Auto:
						valign = -1;
						break;
					case TextAlignment.None:
					case TextAlignment.Start:
						valign = 0;
						break;
					case TextAlignment.Center:
						valign = 0.5;
						break;
					case TextAlignment.End:
						valign = 1.0;
						break;
				}
				SetVerticalTextAlignment("elm.text", valign);
			}
		}

		/// <summary>
		/// Gets or sets the value that indicates whether the text is underlined.
		/// </summary>
		/// <value><c>true</c> if the text is underlined.</value>
		public bool Underline
		{
			get
			{
				return _span.Underline;
			}

			set
			{
				if (value != _span.Underline)
				{
					_span.Underline = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the value that indicates whether the text is striked out.
		/// </summary>
		/// <value><c>true</c> if the text is striked out.</value>
		public bool Strikethrough
		{
			get
			{
				return _span.Strikethrough;
			}

			set
			{
				if (value != _span.Strikethrough)
				{
					_span.Strikethrough = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Implements <see cref="Xamarin.Forms.Platform.Tizen.Native.IMeasurable"/> to provide a desired size of the label.
		/// </summary>
		/// <param name="availableWidth">Available width.</param>
		/// <param name="availableHeight">Available height.</param>
		/// <returns>Size of the control that fits the available area.</returns>
		public ESize Measure(int availableWidth, int availableHeight)
		{
			var size = Geometry;

			Resize(availableWidth, size.Height);

			var formattedSize = Native.TextHelper.GetFormattedTextBlockSize(this);
			Resize(size.Width, size.Height);

			// Set bottom padding for lower case letters that have segments below the bottom line of text (g, j, p, q, y).
			var verticalPadding = (int)Math.Ceiling(0.05 * FontSize);
			formattedSize.Height += verticalPadding;

			// This is the EFL team's guide.
			// For wrap to work properly, the label must be 1 pixel larger than the size of the formatted text.
			formattedSize.Width += 1;

			return formattedSize;
		}

		void ApplyTextAndStyle()
		{
			SetInternalTextAndStyle(_span.GetDecoratedText(), _span.GetStyle());
		}

		void SetInternalTextAndStyle(string formattedText, string textStyle)
		{
			base.Text = formattedText;
			TextStyle = textStyle;
		}
    }
}