Text Area Text length

I’m using a text area but would like to test if the text that is load goes beyond 1 line so that I can change the Height constraint.

How would I go about that?

“beyond one line” encompasses more than just “length”
if it has a few characters, but includes one or more LineFeeds than it exceed one line

What about the kind of used Font ?
Monospaced or variable width ?
ASCII(!) or UTF-8 ?
:wink: or :frowning: ?

sorry… ;-:slight_smile:

A roundabout way of getting at drawn height of text is to let XOJO handle the font stuff, but force the textArea to scroll to the bottommost line of text. Then check the scrollposition when it is scrolled all the way up
Try something like this…

'Getting drawn number of lines text from TextArea

textArea.LineHeight = 20 'MUST DEFINE your lineheight. Otherwise weirdness ensues.

'load some text into textArea
dim a as integer
for a = 1 to 120
  textArea.AppendText str(a) + chr(13)
next a

'measure by setting and getting scrollposition
dim actualScrollPosition as integer
textArea.ScrollPosition = 500000 'set to a ridiculously high value to scroll to last line.
actualScrollPosition = textArea.ScrollPosition 'read it back to get actual scrollposition

dim approxlinesHeight as integer
if actualScrollPosition = 0 then
  approxlinesHeight = textArea.height/textArea.LineHeight
else
  approxlinesHeight = actualScrollPosition + (textArea.height/textArea.LineHeight)
end

'display results of our calculations
textArea.AppendText chr(13)
textArea.AppendText "approxlinesHeight " + str(approxlinesHeight) + chr(13)
textArea.AppendText "textArea.Height " + str(textArea.Height) + chr(13)
textArea.AppendText "textArea.LineHeight " + str(textArea.LineHeight) + chr(13)
textArea.ScrollPosition = 0 'set to a ridiculously high value to scroll to last line

If you just want to see if text fits in the textarea’s height. You can try simply checking if scroll position changes.

textArea.ScrollPosition = 0

'load some text into textArea
dim a as integer
for a = 1 to 3
  textArea.AppendText str(a) + chr(13)
next a

'test by setting and getting scrollposition
dim actualScrollPosition as integer 'just for clarity
textArea.ScrollPosition = 500000 'set to a ridiculously high value to scroll to last line.
actualScrollPosition = textArea.ScrollPosition 'read it back to get actual scrollposition

if actualScrollPosition <> 0 then
  textArea.AppendText chr(13) + "It didn't fit without scrolling"
else
  textArea.AppendText chr(13) + "It fit"
end

textArea.ScrollPosition = 500000 'set to a ridiculously high value to scroll to last line.

Guy, sadly the methods you are using aren’t available for iOSTextArea.

Oh. Well that doesn’t help

Is there a test for the number of lines in an IOSTextArea as there is no scrollposition?