TextView vs Canvas Alignment

First off this is a SWIFT project, but I am pretty sure the same issue exists using Xojo controls.

  1. A Canvas Vertically aligned to the right of a TextView (both the same height)
  2. The Textview contains lines of text in a mono-space font
  3. Attempt to write characterss in the Canvas to line up with the textview text

First… I “could” use another TextView, but I have a future need to “draw” over this “canvas”

The problem is determining two things

  1. How far from the top of the canvas to place the FIRST characters
  2. The line spacing to use

I have expermented with various font sizes attempting to find some combination of the font metrics the would provide values for these two situatons. and I’ve come close, the problem is (I think) partially due to the fact that text is “drawn” at a pixel/point level, but the metrics are DOUBLES

x

In this graphic the cyan is a canvas, the white is a textview, I need to have the “T” in both align…

You can end up with the same issue in Xojo but there its because there can be slight differences in how a text area measures text and how a canvas measures
They may not use the same configuration (kerning, line spacing, leading etc)

I dont know if that plays into it in the same way in Swift but these sure looks like it could be a contributing isue

Ok… got one figured out…
Actual line spacing to be used on the canvas is

floor(useFont.ascender+0.5)+floor(abs(useFont.descender)+0.5)

now to figure the offset from the top… I have figure manually 4 values… but can’t find the commonailty yet.

// fontsize 40 : offset = 9
// fontsize 30 : offset = 7
// fontsize 20 : offset = 6
// fontsize 10 : offset = 2

I thought it was floor(useFont.descender)
but for fontsize 20 that comes out as 4 not 6 :frowning: