Finding Baseline of Text in a TextField

I had often the problem to align drawn text with usual controls. There is no way to calculate the baseline of a text in a textfield by a given font and font size.
It changes by Font and FontSize. Anyhow, to solve me that problem i created a subclassed TextField which has a method ‘CalculateBaseline’ and Properties for TextLeft/TextRight and BaselineTop/BaselineBottom.

Maybe you have some use for it too

look at TEXTASCENT

even with textascent, you can’t calculate the baseline of the text inside the textfield

to be more precise (guess i wasn’t detailed enough before), the distance between the top border of a TextField and the visual/graphical text of the .Text string varies dependent on the TextFont and/or TextSize you use.
Let’s say you have a canvas or picture control right beside a TextField (same top,width,height) and you need to Graphic.DrawString the String on the same x,y so the visual text from the textfield and the visual text inside the canvas/picture is on the same horizontal line. The Textascent nor Textsize will help you to figure out what the distance is from TextField.Top to the visual string in the TextField.
Hope that explains better.

Can you post a picture why this is a problem for you?

Greetings from Rüsselsheim.

[quote=142599:@Beatrix Willius]Can you post a picture why this is a problem for you?

Greetings from Rüsselsheim.[/quote]

Here a Screenshot, 3 Times, same Textfield, same TextSize with just a different FontName. As you can see the ‘Gap’s’ inside the TextField are very different


link didnt work

[quote=142620:@Stefan Welcker]
link didnt work[/quote]

TextAscent works perfectly under Windows but is completely inadequate under Mac OS X, since all it reports is the font size. The only workaround is to measure manually and store the values for the different fonts you plan to use.

It is a bug I reported : <https://xojo.com/issue/36621>

To be clear, that problem has nothing todo with TextAscent as the Top Y of the Text inside a TextField is not related to the TextSize nor the TextAscent, which the above picture proves.

Definition of TextAscent:
Returns the ascent of a line of text drawn with the current font.
TextAscent is the height of the tallest font letter above the font baseline.

Right the text field has a slight margin around the text it shows.
Unfortunately there’s no programmatic way to know what that margin size is.

[quote=142648:@Stefan Welcker]To be clear, that problem has nothing todo with TextAscent as the Top Y of the Text inside a TextField is not related to the TextSize nor the TextAscent, which the above picture proves.

Definition of TextAscent:
Returns the ascent of a line of text drawn with the current font.
TextAscent is the height of the tallest font letter above the font baseline.[/quote]

You do not understand. I have been creating fonts for over 25 years and I assure you it has everything to do with what you want to obtain. The distance between the top of the control and the baseline is the Ascent. See for instance http://encyclopedia2.thefreedictionary.com/Ascent+(font)

Now that value is not available in Mac because of a bug, whereas it is correctly reported in Windows.

Your only option is to measure manually for the fonts you plan to use.

But if you think you know better, I could not care less.

[quote=142653:@Norman Palardy]Right the text field has a slight margin around the text it shows.
Unfortunately there’s no programmatic way to know what that margin size is.[/quote]

Thats what i meant, this margin is even different from TextSize to TextSize and TextFont to TextFont.

I can’t agree, the AscentHeight is the CapHeight + the AscenderHeight, it can’t include the margin to the top of the control, would be meaningless if you have multi rows then, not talking about that the font has no clue where it is used. The Ascent while printing is then including the space to the top of the page? It would be rather a bug if that was the case.

Thats why you have Windows.System.Forms.Padding in Windows, thats the Padding between the Control border and the Control content.

I’m not 100% sure that it varies from font to font or font size to font size