Fix font sizes

This commit is contained in:
Frank A. Krueger 2017-11-26 07:57:32 -08:00
parent 30af34108d
commit e085f236b6
7 changed files with 65 additions and 27 deletions

View File

@ -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);
}
}
}

View File

@ -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;
}
}
}

View File

@ -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 ()

View File

@ -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 ()

View File

@ -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 {

3
Samples/ButtonXamlPage.xaml Executable file → Normal file
View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ButtonXaml.ButtonXamlPage">
@ -7,7 +7,6 @@
<Label Text="Welcome to Xamarin.Forms!"/>
<Label x:Name="LabelCount" Text="Click Count: 0"/>
<Button Text="Tap for click count!"
HeightRequest="30"
Clicked="OnButtonClicked" />
</StackLayout>
</ContentPage>

View File

@ -6,7 +6,6 @@
<StackLayout>
<Label Text="Welcome to DisplayAlert Sample!" />
<Button Text="Tap for Display Alert"
HeightRequest="30"
Clicked="OnButtonClicked" />
</StackLayout>
</ContentPage.Content>