You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.3 KiB

using SkiaSharp;
using SkiaSharp.Views.Maui;
namespace ex_CurvedLabel.Views;
public partial class CurvedLabel : ContentView
{
public CurvedLabel()
{
InitializeComponent();
}
private void OnPainting(object sender, SkiaSharp.Views.Maui.SKPaintSurfaceEventArgs args)
{
SKCanvas canvas = args.Surface.Canvas;
canvas.Clear();
using SKFont font = new SKFont();
font.Size = FontSize;
font.Typeface = SKTypeface.FromFamilyName(FontFamily);
using SKPaint paint = new SKPaint();
paint.Color = TextColor.ToSKColor();
font.MeasureText(Text, out SKRect bounds);
var centerX = bounds.Width / 2;
var startPoint = new SKPoint(centerX - bounds.Width / 2, 100);
var endPoint = new SKPoint(centerX + bounds.Width / 2, 100);
using SKPath path = SKPath.ParseSvgPathData(Path);
path.MoveTo(startPoint);
path.CubicTo(
new SKPoint(centerX - bounds.Width / 4, startPoint.Y + 500),
new SKPoint(centerX + bounds.Width / 4, startPoint.Y + 500),
endPoint);
canvas.DrawTextOnPath(Text, path, 0, 0, font, paint);
}
public static readonly BindableProperty TextProperty
= BindableProperty.Create(
nameof(Text),
typeof(string),
typeof(CurvedLabel),
"something",
propertyChanged: (b, ov, nv) => (b as CurvedLabel)?.canvasView.InvalidateSurface());
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly BindableProperty FontFamilyProperty
= BindableProperty.Create(
nameof(FontFamily),
typeof(string),
typeof(CurvedLabel),
"OpenSans-Regular",
propertyChanged: (b, ov, nv) => (b as CurvedLabel)?.canvasView.InvalidateSurface());
public string FontFamily
{
get { return (string)GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}
public static readonly BindableProperty FontSizeProperty
= BindableProperty.Create(
nameof(FontSize),
typeof(float),
typeof(CurvedLabel),
80.0f,
propertyChanged: (b, ov, nv) => (b as CurvedLabel)?.canvasView.InvalidateSurface());
public float FontSize
{
get { return (float)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
public static readonly BindableProperty TextColorProperty
= BindableProperty.Create(
nameof(TextColor),
typeof(Color),
typeof(CurvedLabel),
Colors.DarkSalmon,
propertyChanged: (b, ov, nv) => (b as CurvedLabel)?.canvasView.InvalidateSurface());
public Color TextColor
{
get { return (Color)GetValue(TextColorProperty); }
set { SetValue(TextColorProperty, value); }
}
public static readonly BindableProperty PathProperty
= BindableProperty.Create(
nameof(Path),
typeof(string),
typeof(CurvedLabel),
"M100,100 1100,1100",
propertyChanged: (b, ov, nv) => (b as CurvedLabel)?.canvasView.InvalidateSurface());
public string Path
{
get { return (string)GetValue(PathProperty); }
set { SetValue(PathProperty, value); }
}
}