diff --git a/ch02_DesigningViews/ex_OutlineText/MainPage.xaml b/ch02_DesigningViews/ex_OutlineText/MainPage.xaml index d3f808d..71895cb 100644 --- a/ch02_DesigningViews/ex_OutlineText/MainPage.xaml +++ b/ch02_DesigningViews/ex_OutlineText/MainPage.xaml @@ -6,14 +6,89 @@ + Padding="4,0" + Spacing="10"> + - diff --git a/ch02_DesigningViews/ex_OutlineText/MainPage.xaml.cs b/ch02_DesigningViews/ex_OutlineText/MainPage.xaml.cs index 2e740b0..b90d1da 100644 --- a/ch02_DesigningViews/ex_OutlineText/MainPage.xaml.cs +++ b/ch02_DesigningViews/ex_OutlineText/MainPage.xaml.cs @@ -9,40 +9,5 @@ public partial class MainPage : ContentPage { InitializeComponent(); } - - private void OnPainting(object sender, SKPaintSurfaceEventArgs args) - { - SKImageInfo info = args.Info; - SKSurface surface = args.Surface; - SKCanvas canvas = surface.Canvas; - - canvas.Clear(); - - string text = "OUTLINE"; - - // Create an SKPaint object to display the text - SKPaint textPaint = new SKPaint - { - Style = SKPaintStyle.Stroke, - StrokeWidth = 1, - FakeBoldText = true, - Color = SKColors.Blue - }; - - // Adjust TextSize property so text is 95% of screen width - float textWidth = textPaint.MeasureText(text); - textPaint.TextSize = 0.95f * info.Width * textPaint.TextSize / textWidth; - - // Find the text bounds - SKRect textBounds = new SKRect(); - textPaint.MeasureText(text, ref textBounds); - - // Calculate offsets to center the text on the screen - float xText = info.Width / 2 - textBounds.MidX; - float yText = info.Height / 2 - textBounds.MidY; - - // And draw the text - canvas.DrawText(text, xText, yText, textPaint); - } } diff --git a/ch02_DesigningViews/ex_OutlineText/MauiProgram.cs b/ch02_DesigningViews/ex_OutlineText/MauiProgram.cs index 51e8c8d..f60ad3b 100644 --- a/ch02_DesigningViews/ex_OutlineText/MauiProgram.cs +++ b/ch02_DesigningViews/ex_OutlineText/MauiProgram.cs @@ -15,6 +15,7 @@ public static class MauiProgram { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); + fonts.AddFont("Friendfship Magic - Equestria.ttf", "FriendfshipMagicEquestria"); }); #if DEBUG diff --git a/ch02_DesigningViews/ex_OutlineText/Resources/Fonts/Friendfship Magic - Equestria.ttf b/ch02_DesigningViews/ex_OutlineText/Resources/Fonts/Friendfship Magic - Equestria.ttf new file mode 100755 index 0000000..a1250ae Binary files /dev/null and b/ch02_DesigningViews/ex_OutlineText/Resources/Fonts/Friendfship Magic - Equestria.ttf differ diff --git a/ch02_DesigningViews/ex_OutlineText/Views/LabelOutline.xaml b/ch02_DesigningViews/ex_OutlineText/Views/LabelOutline.xaml index cc27e7f..fcdb41b 100644 --- a/ch02_DesigningViews/ex_OutlineText/Views/LabelOutline.xaml +++ b/ch02_DesigningViews/ex_OutlineText/Views/LabelOutline.xaml @@ -2,8 +2,9 @@ + x:Class="ex_OutlineText.Views.LabelOutline" + x:Name="root"> + HeightRequest="{Binding ActualHeight, x:Reference=root}" + WidthRequest="{Binding ActualWidth, x:Reference=root}"/> diff --git a/ch02_DesigningViews/ex_OutlineText/Views/LabelOutline.xaml.cs b/ch02_DesigningViews/ex_OutlineText/Views/LabelOutline.xaml.cs index d36edac..b7a8d88 100644 --- a/ch02_DesigningViews/ex_OutlineText/Views/LabelOutline.xaml.cs +++ b/ch02_DesigningViews/ex_OutlineText/Views/LabelOutline.xaml.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using SkiaSharp; using SkiaSharp.Views.Maui; @@ -58,6 +59,54 @@ public partial class LabelOutline : ContentView set { SetValue(StrokeWidthProperty, value); } } + public static readonly BindableProperty FontFamilyProperty + = BindableProperty.Create(nameof(FontFamily), + typeof(string), + typeof(LabelOutline), + "something"); + + public string FontFamily + { + get { return (string)GetValue(FontFamilyProperty); } + set { SetValue(FontFamilyProperty, value); } + } + + public static readonly BindableProperty TextSizeProperty + = BindableProperty.Create(nameof(TextSize), + typeof(float), + typeof(LabelOutline), + 24.0f); + + public float TextSize + { + get { return (float)GetValue(TextSizeProperty); } + set { SetValue(TextSizeProperty, value); } + } + + public static readonly BindableProperty AspectProperty + = BindableProperty.Create(nameof(Aspect), + typeof(Aspect), + typeof(LabelOutline), + Aspect.Center); + + public Aspect Aspect + { + get { return (Aspect)GetValue(AspectProperty); } + set { SetValue(AspectProperty, value); } + } + + public static readonly BindableProperty RatioProperty + = BindableProperty.Create(nameof(Ratio), + typeof(float), + typeof(LabelOutline), + 0.95f); + + public float Ratio + { + get { return (float)GetValue(RatioProperty); } + set { SetValue(RatioProperty, value); } + } + private void OnPainting(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; @@ -74,12 +123,33 @@ public partial class LabelOutline : ContentView Style = SKPaintStyle.Fill, StrokeWidth = StrokeWidth, FakeBoldText = true, - Color = Fill.ToSKColor() + Typeface = SKTypeface.FromFamilyName(FontFamily), + Color = Fill.ToSKColor(), + TextSize = TextSize }; + switch(Aspect) + { + case Aspect.AspectFit: + float textHeight = Ratio * info.Height; + float textWidth2 = textPaint.MeasureText(text); + textWidth2 = Ratio * info.Width * textPaint.TextSize / textWidth2; + textPaint.TextSize = Math.Min(textHeight, textWidth2); + Console.WriteLine($"info.Width: {info.Width} ; info.Height: {info.Height} ; textPaint.TextSize: {textPaint.TextSize} ; textHeight: {textHeight} ; textWidth: {textWidth2}"); + break; + case Aspect.AspectFill: + case Aspect.Fill: + float textWidth = textPaint.MeasureText(text); + textPaint.TextSize = Ratio * info.Width * textPaint.TextSize / textWidth; + Console.WriteLine($"info.Width: {info.Width} ; info.Height: {info.Height} ; textPaint.TextSize: {textPaint.TextSize}"); + break; + case Aspect.Center: + default: + break; + } // Adjust TextSize property so text is 95% of screen width - float textWidth = textPaint.MeasureText(text); - textPaint.TextSize = 0.95f * info.Width * textPaint.TextSize / textWidth; + // float textWidth = textPaint.MeasureText(text); + // textPaint.TextSize = 0.95f * info.Width * textPaint.TextSize / textWidth; // Find the text bounds SKRect textBounds = new SKRect(); diff --git a/ch02_DesigningViews/ex_ShadowLabel/MainPage.xaml b/ch02_DesigningViews/ex_ShadowLabel/MainPage.xaml index e56dcbd..c023857 100644 --- a/ch02_DesigningViews/ex_ShadowLabel/MainPage.xaml +++ b/ch02_DesigningViews/ex_ShadowLabel/MainPage.xaml @@ -6,17 +6,17 @@ + Spacing="25" + BackgroundColor="LightSalmon">