2016-03-22 23:02:25 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
using Windows.UI.Xaml.Documents;
|
|
|
|
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
|
|
|
|
namespace Xamarin.Forms.Platform.UWP
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
namespace Xamarin.Forms.Platform.WinRT
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
public static class FormattedStringExtensions
|
|
|
|
|
{
|
|
|
|
|
public static Run ToRun(this Span span)
|
|
|
|
|
{
|
2017-05-04 18:32:21 +03:00
|
|
|
|
var run = new Run { Text = span.Text ?? string.Empty };
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
|
|
if (span.ForegroundColor != Color.Default)
|
|
|
|
|
run.Foreground = span.ForegroundColor.ToBrush();
|
|
|
|
|
|
|
|
|
|
if (!span.IsDefault())
|
2016-04-11 19:50:28 +03:00
|
|
|
|
#pragma warning disable 618
|
2016-03-22 23:02:25 +03:00
|
|
|
|
run.ApplyFont(span.Font);
|
2016-04-11 19:50:28 +03:00
|
|
|
|
#pragma warning restore 618
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
|
|
return run;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class LabelRenderer : ViewRenderer<Label, TextBlock>
|
|
|
|
|
{
|
|
|
|
|
bool _fontApplied;
|
2016-08-30 21:47:09 +03:00
|
|
|
|
bool _isInitiallyDefault;
|
2017-03-14 13:51:28 +03:00
|
|
|
|
SizeRequest _perfectSize;
|
|
|
|
|
bool _perfectSizeValid;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
|
|
protected override Windows.Foundation.Size ArrangeOverride(Windows.Foundation.Size finalSize)
|
|
|
|
|
{
|
|
|
|
|
if (Element == null)
|
|
|
|
|
return finalSize;
|
|
|
|
|
double childHeight = Math.Max(0, Math.Min(Element.Height, Control.DesiredSize.Height));
|
|
|
|
|
var rect = new Rect();
|
|
|
|
|
|
|
|
|
|
switch (Element.VerticalTextAlignment)
|
|
|
|
|
{
|
|
|
|
|
case TextAlignment.Start:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
case TextAlignment.Center:
|
|
|
|
|
rect.Y = (int)((finalSize.Height - childHeight) / 2);
|
|
|
|
|
break;
|
|
|
|
|
case TextAlignment.End:
|
|
|
|
|
rect.Y = finalSize.Height - childHeight;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
rect.Height = childHeight;
|
|
|
|
|
rect.Width = finalSize.Width;
|
|
|
|
|
Control.Arrange(rect);
|
|
|
|
|
return finalSize;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-14 13:51:28 +03:00
|
|
|
|
public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
|
|
|
|
|
{
|
|
|
|
|
if (!_perfectSizeValid)
|
|
|
|
|
{
|
|
|
|
|
_perfectSize = base.GetDesiredSize(double.PositiveInfinity, double.PositiveInfinity);
|
|
|
|
|
_perfectSize.Minimum = new Size(Math.Min(10, _perfectSize.Request.Width), _perfectSize.Request.Height);
|
|
|
|
|
_perfectSizeValid = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 13:43:17 +03:00
|
|
|
|
var widthFits = widthConstraint >= _perfectSize.Request.Width;
|
|
|
|
|
var heightFits = heightConstraint >= _perfectSize.Request.Height;
|
|
|
|
|
|
|
|
|
|
if (widthFits && heightFits)
|
2017-03-14 13:51:28 +03:00
|
|
|
|
return _perfectSize;
|
|
|
|
|
|
|
|
|
|
var result = base.GetDesiredSize(widthConstraint, heightConstraint);
|
2017-03-22 13:43:17 +03:00
|
|
|
|
var tinyWidth = Math.Min(10, result.Request.Width);
|
|
|
|
|
result.Minimum = new Size(tinyWidth, result.Request.Height);
|
|
|
|
|
|
|
|
|
|
if (widthFits || Element.LineBreakMode == LineBreakMode.NoWrap)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
bool containerIsNotInfinitelyWide = !double.IsInfinity(widthConstraint);
|
|
|
|
|
|
|
|
|
|
if (containerIsNotInfinitelyWide)
|
2017-03-14 13:51:28 +03:00
|
|
|
|
{
|
2017-03-22 13:43:17 +03:00
|
|
|
|
bool textCouldHaveWrapped = Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap;
|
|
|
|
|
bool textExceedsContainer = result.Request.Width > widthConstraint;
|
|
|
|
|
|
|
|
|
|
if (textExceedsContainer || textCouldHaveWrapped)
|
|
|
|
|
{
|
|
|
|
|
var expandedWidth = Math.Max(tinyWidth, widthConstraint);
|
|
|
|
|
result.Request = new Size(expandedWidth, result.Request.Height);
|
|
|
|
|
}
|
2017-03-14 13:51:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
|
|
|
|
|
{
|
|
|
|
|
base.OnElementChanged(e);
|
|
|
|
|
|
|
|
|
|
if (e.NewElement != null)
|
|
|
|
|
{
|
|
|
|
|
if (Control == null)
|
|
|
|
|
{
|
|
|
|
|
SetNativeControl(new TextBlock());
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 21:47:09 +03:00
|
|
|
|
_isInitiallyDefault = Element.IsDefault();
|
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
UpdateText(Control);
|
|
|
|
|
UpdateColor(Control);
|
|
|
|
|
UpdateAlign(Control);
|
|
|
|
|
UpdateFont(Control);
|
|
|
|
|
UpdateLineBreakMode(Control);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
2016-09-12 14:52:31 +03:00
|
|
|
|
if (e.PropertyName == Label.TextProperty.PropertyName || e.PropertyName == Label.FormattedTextProperty.PropertyName)
|
2016-03-22 23:02:25 +03:00
|
|
|
|
UpdateText(Control);
|
|
|
|
|
else if (e.PropertyName == Label.TextColorProperty.PropertyName)
|
|
|
|
|
UpdateColor(Control);
|
|
|
|
|
else if (e.PropertyName == Label.HorizontalTextAlignmentProperty.PropertyName || e.PropertyName == Label.VerticalTextAlignmentProperty.PropertyName)
|
|
|
|
|
UpdateAlign(Control);
|
|
|
|
|
else if (e.PropertyName == Label.FontProperty.PropertyName)
|
|
|
|
|
UpdateFont(Control);
|
|
|
|
|
else if (e.PropertyName == Label.LineBreakModeProperty.PropertyName)
|
|
|
|
|
UpdateLineBreakMode(Control);
|
|
|
|
|
|
|
|
|
|
base.OnElementPropertyChanged(sender, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateAlign(TextBlock textBlock)
|
|
|
|
|
{
|
2017-03-14 13:51:28 +03:00
|
|
|
|
_perfectSizeValid = false;
|
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
if (textBlock == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Label label = Element;
|
|
|
|
|
if (label == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
textBlock.TextAlignment = label.HorizontalTextAlignment.ToNativeTextAlignment();
|
|
|
|
|
textBlock.VerticalAlignment = label.VerticalTextAlignment.ToNativeVerticalAlignment();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateColor(TextBlock textBlock)
|
|
|
|
|
{
|
|
|
|
|
if (textBlock == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Label label = Element;
|
|
|
|
|
if (label != null && label.TextColor != Color.Default)
|
|
|
|
|
{
|
|
|
|
|
textBlock.Foreground = label.TextColor.ToBrush();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
textBlock.ClearValue(TextBlock.ForegroundProperty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateFont(TextBlock textBlock)
|
|
|
|
|
{
|
2017-03-14 13:51:28 +03:00
|
|
|
|
_perfectSizeValid = false;
|
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
if (textBlock == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Label label = Element;
|
|
|
|
|
if (label == null || (label.IsDefault() && !_fontApplied))
|
|
|
|
|
return;
|
|
|
|
|
|
2016-04-11 19:50:28 +03:00
|
|
|
|
#pragma warning disable 618
|
2016-08-30 21:47:09 +03:00
|
|
|
|
Font fontToApply = label.IsDefault() && _isInitiallyDefault ? Font.SystemFontOfSize(NamedSize.Medium) : label.Font;
|
2016-04-11 19:50:28 +03:00
|
|
|
|
#pragma warning restore 618
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
|
|
textBlock.ApplyFont(fontToApply);
|
|
|
|
|
_fontApplied = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateLineBreakMode(TextBlock textBlock)
|
|
|
|
|
{
|
2017-03-14 13:51:28 +03:00
|
|
|
|
_perfectSizeValid = false;
|
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
if (textBlock == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
switch (Element.LineBreakMode)
|
|
|
|
|
{
|
|
|
|
|
case LineBreakMode.NoWrap:
|
|
|
|
|
textBlock.TextTrimming = TextTrimming.Clip;
|
|
|
|
|
textBlock.TextWrapping = TextWrapping.NoWrap;
|
|
|
|
|
break;
|
|
|
|
|
case LineBreakMode.WordWrap:
|
|
|
|
|
textBlock.TextTrimming = TextTrimming.None;
|
|
|
|
|
textBlock.TextWrapping = TextWrapping.Wrap;
|
|
|
|
|
break;
|
|
|
|
|
case LineBreakMode.CharacterWrap:
|
|
|
|
|
textBlock.TextTrimming = TextTrimming.WordEllipsis;
|
|
|
|
|
textBlock.TextWrapping = TextWrapping.Wrap;
|
|
|
|
|
break;
|
|
|
|
|
case LineBreakMode.HeadTruncation:
|
2017-03-14 13:51:28 +03:00
|
|
|
|
// TODO: This truncates at the end.
|
2016-03-22 23:02:25 +03:00
|
|
|
|
textBlock.TextTrimming = TextTrimming.WordEllipsis;
|
|
|
|
|
textBlock.TextWrapping = TextWrapping.NoWrap;
|
|
|
|
|
break;
|
|
|
|
|
case LineBreakMode.TailTruncation:
|
2016-08-30 21:12:27 +03:00
|
|
|
|
textBlock.TextTrimming = TextTrimming.CharacterEllipsis;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
textBlock.TextWrapping = TextWrapping.NoWrap;
|
|
|
|
|
break;
|
|
|
|
|
case LineBreakMode.MiddleTruncation:
|
2017-03-14 13:51:28 +03:00
|
|
|
|
// TODO: This truncates at the end.
|
2016-03-22 23:02:25 +03:00
|
|
|
|
textBlock.TextTrimming = TextTrimming.WordEllipsis;
|
|
|
|
|
textBlock.TextWrapping = TextWrapping.NoWrap;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateText(TextBlock textBlock)
|
|
|
|
|
{
|
2017-03-14 13:51:28 +03:00
|
|
|
|
_perfectSizeValid = false;
|
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
|
if (textBlock == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Label label = Element;
|
|
|
|
|
if (label != null)
|
|
|
|
|
{
|
|
|
|
|
FormattedString formatted = label.FormattedText;
|
|
|
|
|
|
|
|
|
|
if (formatted == null)
|
|
|
|
|
{
|
|
|
|
|
textBlock.Text = label.Text ?? string.Empty;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
textBlock.Inlines.Clear();
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < formatted.Spans.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
textBlock.Inlines.Add(formatted.Spans[i].ToRun());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|