Michael Esteves
Michael Esteves

Reputation: 1485

Skiasharp text rendering off canvas

I'm creating a custom control with skiasharp and xamarin forms. Something I've run into is when I try draw text in the top left corner of my canvas I have to offset the y co ord in order for the text to render within the canvas. I'd expect the x y co ord of the text to be in the same position as the x y co ord of the rect. What am I missing?

Using Skiasharp.View.Forms - v1.68.1 off nuget

Thanks!

protected override void OnPaintSurface(SKPaintSurfaceEventArgs args)
{
  base.OnPaintSurface(args);

  SKImageInfo info = args.Info;
  SKSurface surface = args.Surface;
  SKCanvas canvas = surface.Canvas;

  canvas.Clear();
  canvas.Save();

  using var paint = new SKPaint {IsAntialias = true, Color = Color.Black.ToSKColor(), Style = SKPaintStyle.Stroke};

  canvas.DrawRect(0, 0, info.Width, info.Height, paint);

  canvas.DrawText("Off screen", 0, 0, paint);
  canvas.DrawText("On screen", 0, 10, paint);

  canvas.Restore();
}

Upvotes: 1

Views: 1718

Answers (1)

ColeX
ColeX

Reputation: 14475

You could set y coordinate as SKPaint.FontSpacing , it is the recommend line spacing.

 canvas.DrawText("SKCanvasView Height and Width:", 0, paint.FontSpacing, paint);

Refer https://github.com/xamarin/xamarin-forms-samples/blob/f7cc661331b5db605e64e7efe83ef2b9fc644afb/SkiaSharpForms/Demos/Demos/SkiaSharpFormsDemos/Basics/SurfaceSizePage.cs#L38

Upvotes: 3

Related Questions