From f6bdb76b0a7803af633b28e7fd2dac1d08b419e5 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Thu, 22 Jun 2017 11:49:41 +0200 Subject: [iOS] decoupling UpdateText() on LabelRenderer (#950) * [iOS] decoupling UpdateText() on LabelRenderer * [iOS] do not update font and color on formattedstrings --- .../Renderers/LabelRenderer.cs | 67 +++++++++++++++++----- 1 file changed, 52 insertions(+), 15 deletions(-) (limited to 'Xamarin.Forms.Platform.iOS') diff --git a/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs index 5eef6915..0d016e50 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs @@ -6,12 +6,14 @@ using SizeF = CoreGraphics.CGSize; #if __MOBILE__ using UIKit; using NativeLabel = UIKit.UILabel; - -namespace Xamarin.Forms.Platform.iOS #else using AppKit; using NativeLabel = AppKit.NSTextField; +#endif +#if __MOBILE__ +namespace Xamarin.Forms.Platform.iOS +#else namespace Xamarin.Forms.Platform.MacOS #endif { @@ -110,6 +112,9 @@ namespace Xamarin.Forms.Platform.MacOS } UpdateText(); + UpdateTextColor(); + UpdateFont(); + UpdateLineBreakMode(); UpdateAlignment(); } @@ -126,9 +131,9 @@ namespace Xamarin.Forms.Platform.MacOS else if (e.PropertyName == Label.VerticalTextAlignmentProperty.PropertyName) UpdateLayout(); else if (e.PropertyName == Label.TextColorProperty.PropertyName) - UpdateText(); + UpdateTextColor(); else if (e.PropertyName == Label.FontProperty.PropertyName) - UpdateText(); + UpdateFont(); else if (e.PropertyName == Label.TextProperty.PropertyName) UpdateText(); else if (e.PropertyName == Label.FormattedTextProperty.PropertyName) @@ -241,6 +246,7 @@ namespace Xamarin.Forms.Platform.MacOS #endif } + bool isTextFormatted; void UpdateText() { _perfectSizeValid = false; @@ -251,26 +257,57 @@ namespace Xamarin.Forms.Platform.MacOS { #if __MOBILE__ Control.AttributedText = formatted.ToAttributed(Element, (Color)values[2]); - } - else - { - Control.Text = (string)values[1]; - // default value of color documented to be black in iOS docs - Control.Font = Element.ToUIFont(); - Control.TextColor = ((Color)values[2]).ToUIColor(ColorExtensions.Black); - } #else Control.AttributedStringValue = formatted.ToAttributed(Element, (Color)values[2]); +#endif + isTextFormatted = true; } else { + if (isTextFormatted) + { + UpdateFont(); + UpdateTextColor(); + } +#if __MOBILE__ + Control.Text = (string)values[1]; +#else Control.StringValue = (string)values[1] ?? ""; - // default value of color documented to be black in iOS docs - Control.Font = Element.ToNSFont(); - Control.TextColor = ((Color)values[2]).ToNSColor(ColorExtensions.Black); +#endif + isTextFormatted = false; } + UpdateLayout(); + } + + void UpdateFont() + { + if(isTextFormatted) + return; + _perfectSizeValid = false; + +#if __MOBILE__ + Control.Font = Element.ToUIFont(); +#else + Control.Font = Element.ToNSFont(); #endif + UpdateLayout(); + } + + void UpdateTextColor() + { + if (isTextFormatted) + return; + + _perfectSizeValid = false; + + var textColor = (Color)Element.GetValue(Label.TextColorProperty); + // default value of color documented to be black in iOS docs +#if __MOBILE__ + Control.TextColor = textColor.ToUIColor(ColorExtensions.Black); +#else + Control.TextColor = textColor.ToNSColor(ColorExtensions.Black); +#endif UpdateLayout(); } -- cgit v1.2.3