From e085f236b6b201aef5b3c1d799c1bf97e3fda686 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Sun, 26 Nov 2017 07:57:32 -0800 Subject: [PATCH] Fix font sizes --- Ooui.Forms/Extensions/FontExtensions.cs | 61 +++++++++++++++++++------ Ooui.Forms/Renderers/ButtonRenderer.cs | 17 ++++--- Ooui.Forms/Renderers/EntryRenderer.cs | 2 +- Ooui.Forms/Renderers/LabelRenderer.cs | 6 +-- Ooui/Style.cs | 2 +- Samples/ButtonXamlPage.xaml | 3 +- Samples/DisplayAlertPage.xaml | 1 - 7 files changed, 65 insertions(+), 27 deletions(-) mode change 100755 => 100644 Samples/ButtonXamlPage.xaml diff --git a/Ooui.Forms/Extensions/FontExtensions.cs b/Ooui.Forms/Extensions/FontExtensions.cs index 765ee02..310ece6 100644 --- a/Ooui.Forms/Extensions/FontExtensions.cs +++ b/Ooui.Forms/Extensions/FontExtensions.cs @@ -5,8 +5,55 @@ namespace Ooui.Forms.Extensions { public static class FontExtensions { - public static void SetStyleFont (this View view, Style style) + public static void SetStyleFont (this View view, string family, double size, FontAttributes attrs, Style style) { +#pragma warning disable RECS0018 // Comparison of floating point numbers with equality operator + if (size == 14.0) { + style.FontSize = null; + } + else { + style.FontSize = size; + } +#pragma warning restore RECS0018 // Comparison of floating point numbers with equality operator + + if (string.IsNullOrEmpty (family)) { + style.FontFamily = null; + } + else { + style.FontFamily = family; + } + + if (attrs.HasFlag (FontAttributes.Bold)) { + style.FontWeight = "bold"; + } + else { + style.FontWeight = null; + } + + if (attrs.HasFlag (FontAttributes.Italic)) { + style.FontStyle = "italic"; + } + else { + style.FontStyle = null; + } + } + + public static Size MeasureSize (this string text, string fontFamily, double fontSize, FontAttributes fontAttrs) + { + if (string.IsNullOrEmpty (text)) + return Size.Zero; + + var fontHeight = fontSize; + var charWidth = fontSize * 0.5; + + var width = text.Length * charWidth; + + return new Size (width, fontHeight); + } + + public static Size MeasureSize (this string text, Style style) + { + return MeasureSize (text, "", 14, FontAttributes.None); } public static string ToOouiTextAlign (this TextAlignment align) @@ -22,17 +69,5 @@ namespace Ooui.Forms.Extensions } } - public static Size MeasureSize (this string text, Style style) - { - if (string.IsNullOrEmpty (text)) - return Size.Zero; - - var fontHeight = 16.0; - var charWidth = 8.0; - - var width = text.Length * charWidth; - - return new Size (width, fontHeight); - } } } diff --git a/Ooui.Forms/Renderers/ButtonRenderer.cs b/Ooui.Forms/Renderers/ButtonRenderer.cs index 751e6c7..727d835 100644 --- a/Ooui.Forms/Renderers/ButtonRenderer.cs +++ b/Ooui.Forms/Renderers/ButtonRenderer.cs @@ -12,6 +12,13 @@ namespace Ooui.Forms.Renderers Ooui.Color _buttonTextColorDefaultHighlighted; Ooui.Color _buttonTextColorDefaultNormal; + public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint) + { + var size = Element.Text.MeasureSize (Element.FontFamily, Element.FontSize, Element.FontAttributes); + size = new Size (size.Width, size.Height * 1.428 + 14); + return new SizeRequest (size, size); + } + protected override void Dispose (bool disposing) { if (Control != null) { @@ -31,6 +38,8 @@ namespace Ooui.Forms.Renderers Debug.Assert (Control != null, "Control != null"); + Control.ClassName = "btn btn-primary"; + _buttonTextColorDefaultNormal = Ooui.Colors.Black; _buttonTextColorDefaultHighlighted = Ooui.Colors.Black; _buttonTextColorDefaultDisabled = Ooui.Colors.Black; @@ -98,7 +107,7 @@ namespace Ooui.Forms.Renderers void UpdateFont () { - Element.SetStyleFont (Control.Style); + Element.SetStyleFont (Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style); } void UpdateImage () @@ -141,14 +150,10 @@ namespace Ooui.Forms.Renderers void UpdateTextColor () { if (Element.TextColor == Xamarin.Forms.Color.Default) { - Control.Style.Color = _buttonTextColorDefaultNormal; - Control.Style.Color = _buttonTextColorDefaultHighlighted; - Control.Style.Color = _buttonTextColorDefaultDisabled; + Control.Style.Color = null; } else { Control.Style.Color = Element.TextColor.ToOouiColor (); - Control.Style.Color = Element.TextColor.ToOouiColor (); - Control.Style.Color = _buttonTextColorDefaultDisabled; } } } diff --git a/Ooui.Forms/Renderers/EntryRenderer.cs b/Ooui.Forms/Renderers/EntryRenderer.cs index d48f78e..e72843d 100644 --- a/Ooui.Forms/Renderers/EntryRenderer.cs +++ b/Ooui.Forms/Renderers/EntryRenderer.cs @@ -140,7 +140,7 @@ namespace Ooui.Forms.Renderers initialSize = testString.MeasureSize (Control.Style); } - Element.SetStyleFont (Control.Style); + Element.SetStyleFont (Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style); } void UpdateKeyboard () diff --git a/Ooui.Forms/Renderers/LabelRenderer.cs b/Ooui.Forms/Renderers/LabelRenderer.cs index 402563e..6a6104d 100644 --- a/Ooui.Forms/Renderers/LabelRenderer.cs +++ b/Ooui.Forms/Renderers/LabelRenderer.cs @@ -17,8 +17,8 @@ namespace Ooui.Forms.Renderers 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); + var size = Element.Text.MeasureSize (Element.FontFamily, Element.FontSize, Element.FontAttributes); + _perfectSize = new SizeRequest (size, size); _perfectSizeValid = true; } @@ -157,7 +157,7 @@ namespace Ooui.Forms.Renderers return; _perfectSizeValid = false; - Element.SetStyleFont (Control.Style); + Element.SetStyleFont (Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style); } void UpdateTextColor () diff --git a/Ooui/Style.cs b/Ooui/Style.cs index cec8577..06b6149 100644 --- a/Ooui/Style.cs +++ b/Ooui/Style.cs @@ -180,7 +180,7 @@ namespace Ooui public Value FontSize { get => this["font-size"]; - set => this["font-size"] = value; + set => this["font-size"] = AddNumberUnits (value, "px"); } public Value FontStyle { diff --git a/Samples/ButtonXamlPage.xaml b/Samples/ButtonXamlPage.xaml old mode 100755 new mode 100644 index 02012d6..9fafa5f --- a/Samples/ButtonXamlPage.xaml +++ b/Samples/ButtonXamlPage.xaml @@ -1,4 +1,4 @@ - + @@ -7,7 +7,6 @@