Bug in results from TextArea. LineNumber(pos) if last line of text has less than two characters

Feedback Case Number: https://tracker.xojo.com/xojoinc/xojo/-/issues/72962

AFAIK, there’s no “LineCount” method for TextArea, and instead we need to use .LineNumber(lengthOfText) + 1 to get the line count. Except the method returns incorrect results if the last line is has less than two characters.

Might be similar to this report, verified, but it’s over seven years old: https://tracker.xojo.com/xojoinc/xojo/-/issues/43874 but that bug is related to a function in the older version of the TextArea control.

Also on the forums:

but the reported workaround (add 2 to position) doesn’t work. (First character in TextArea has wrong LineNumber - #3 by David_Cox )

I don’t think the function has ever worked correctly since the transition to Cocoa. I logged a similar issue #51461 several years ago which was found when we moved from RB to Xojo.

If you have the MBS plugins the following might solve the problem for macOS.

  Dim textViewObj As NSTextViewMBS
  Dim layoutManagerObj As NSLayoutManagerMBS
  Dim glyphCount As Int32
  Dim index As Int32
  Dim rowCount As Int32
  Dim lineRangeObj As NSRangeMBS
  textViewObj = TextArea1.NSTextViewMBS
  layoutManagerObj = textViewObj.layoutManager
  glyphCount = layoutManagerObj.glyphRangeForTextContainer(textViewObj.textContainer).MaxRange
  index = 0
  rowCount = 0
  While (rowCount < glyphCount) And (index < glyphCount)
    Call layoutManagerObj.lineFragmentRectForGlyphAtIndex(index, lineRangeObj)
    index = lineRangeObj.MaxRange
    rowCount = rowCount + 1
  System.DebugLog Str(rowCount)
1 Like