# Calculating Possible Number of Lines in Text Area

Hi,

I’ve just started playing around with Xojo. I’m trying to determine the potential number of lines of text that a TextArea can display before scrolling. I’m trying something along these lines:

textArea.FontUnit = FontUnits.Pixel
textArea.FontSize = 18
numLines = textArea.Height / textArea.FontSize

This has the issue that I don’t know the pixel spacing between lines, but the result I’m seeing is less lines that the TextArea can show… which suggests that the textArea.FontSize denominator is bigger than it should be even without the lines pacing. So, I’m guessing either height is the wrong thing to use or the fontsize is.

Is there a more robust way that someone can suggest? Especially if the text area is resized by a user on a desktop.

Carl C.

Hi @Carl_Cavanagh, my suggestion is:

``````Static oPic As Picture
Static oGraphics As Graphics

If oPic Is Nil Then
oPic = New Picture(1, 1)
oGraphics = oPic.Graphics
End If

oGraphics.FontName = YourTextArea.FontName
oGraphics.FontSize = YourTextArea.FontSize

Var dLineHeight As Double = oGraphics.TextHeight * YourTextArea.LineSpacing
Var iLinesAvailable As Integer = YourTextArea.Height / dLineHeight
``````

Thanks @Martin_T Martin, I gave that a try and I think the TextArea.Height must be returning something unexpected. Here is a screen shot of the numbers involved in the calculation:

Here is a screen shot of the textarea, that is showing that it looks like it has space for another 20 lines.

Note: screen shots scaled down to make them fit better into this conversation.

All, the font and text heights look correct so I’m guessing the text area height must be in different units or maybe there is some kind of scaling factor somewhere for screen resolution??

Not sure, I’ll keep digging through the documentation.

Hmm, this code works fine on macOS:

``````Var YourTextArea As TextArea = TextArea1

Static oPic As Picture
Static oGraphics As Graphics

If oPic Is Nil Then
oPic = New Picture(1, 1)
oGraphics = oPic.Graphics
End If

oGraphics.FontName = YourTextArea.FontName
oGraphics.FontSize = YourTextArea.FontSize

Var dLineHeight As Double = oGraphics.TextHeight * YourTextArea.LineSpacing
Var iLinesAvailable As Integer = YourTextArea.Height / dLineHeight

Var ars() As String

For i As Integer = 1 To iLinesAvailable

Next i

YourTextArea.Text = String.FromArray(ars, EndOfLine)
``````

Ah ha, it gets pretty darn close if I use:

Var iLinesAvailable As Integer = (YourTextArea.Height * YourApp.ScaleFactor) / dLineHeight

Using this on macOS results in the double count of the lines, which is wrong. So looks like an OS specific detail.

I’m using a desktop build on Windows 10, maybe the HiDPI support is different between Windows and OSX, I’m literally a couple of days in using Xojo so not super knowledegable on what backend differences there are.

@Martin_T Thanks for the support!! Much appreciated, I was running out of ideas on my end! Carl c.

1 Like

You can handle it this way:

``````Var iLinesAvailable As Integer

#If TargetWindows
iLinesAvailable = (YourTextArea.Height * YourApp.ScaleFactor) / dLineHeight
#ElseIf TargetMacOS
iLinesAvailable = YourTextArea.Height / dLineHeight
#EndIf
``````

This takes into account in the code the platform for which the app is built.