Text Area Text length

  1. 2 years ago

    Martin F

    26 May 2018 Pre-Release Testers, Xojo Pro Kiama, Australia

    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?

  2. Dave S

    26 May 2018 San Diego, California USA

    "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

  3. Emile S

    26 May 2018 Europe (France, Strasbourg)

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

    sorry… ;-:)

  4. Guy K

    26 May 2018 Pre-Release Testers
    Edited 2 years ago

    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.
  5. Art G

    26 May 2018 Prescott AZ

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

  6. Guy K

    26 May 2018 Pre-Release Testers

    Oh. Well that doesn't help

  7. Martin F

    26 May 2018 Pre-Release Testers, Xojo Pro Kiama, Australia

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

or Sign Up to reply!