Number of lines of text in Label

In a Label I can set Multiline = True,
But, can I know how many lines my text uses in this label?

I think you should be able to figure that out by using Graphics.StringHeight( Text as String, WrapWidth as Integer ) As Integer.

as long as you are not using STYLEDTEXT or you are using a single FONT… mix fonts and this doesn’t work anymore

Thanks Maximilian,

But my Label is on a window, and Graphics is a property of a Canvas and I have no Canvas. Besides Graphics is deprecated.
Is there no other way?

Label? then it can only be one font.

cnt=countfields(label.text,endofline)

Dave,

This is a good approach, but text is wrapped automatically in the Label, without EndOfLine, so I still can not know how many lines I have.
Well, in fact what I want is to resize the label to show all the text.
Is there another way to do it without knowing the number of lines?

FUNCTION Measure(lbl as label) as integer
  Dim p As Picture
  Dim g As graphics
  Dim v(-1) As String
  Dim i As Integer
  Dim cnt as  integer
  v=Split(lbl.text,endofline)
  p=New Picture(32,32,32)
  g=p.Graphics
  g.TextFont=lbl.TextFont
  g.TextSize=lbl.TextSize
  g.Bold=lbl.bold
  g.Italic=lbl.Italic
  cnt=0
  For i=0 To v.Ubound
    w=g.StringWidth(v(i))
   cnt=cnt+floor(w/label.width)
  Next i
return cnt
END FUNCTION

Try this… (UNTESTED)

[quote=110465:@Ramon SASTRE]Dave,

This is a good approach, but text is wrapped automatically in the Label, without EndOfLine, so I still can not know how many lines I have.
Well, in fact what I want is to resize the label to show all the text.
Is there another way to do it without knowing the number of lines?[/quote]

Ramon, Where did you read Graphics was deprecated ? ? ? No mention in the LR, and if it where, how would Xojo manipulate graphic objects…

Sub Action() Graphics.TextFont = Label1.TextFont Graphics.TextSize = Label1.TextSize msgbox str(Floor(Graphics.StringHeight( Label1.Text, Label1.width)/Graphics.StringHeight("N",20))) End Sub

I believe the misconception is… “GRAPHICS access outside of the PAINT event of a CANVAS is deprecated”

Michel… in your Action Event… where does GRAPHICS come from? A Label has no such property

If you still do not like that method, the alternative is to wrap lines yourself. The general principle is :

  • Fetch a substring of the text about the number of characters needed to fill the whole width of the label. You may simply count on screen
  • Look with Mid from the width of the substring until you get to a space
  • Count one line and fetch a new substring from the space
  • Start over

If a substring does not contain any space, consider the substring as one line that has been truncated.

I used that somewhere but I cannot locate the code at this moment. If you absolutely require it I will try to find it, but I strongly advise the graphics method for its simplicity.

[quote=110468:@Dave S]I believe the misconception is… “GRAPHICS access outside of the PAINT event of a CANVAS is deprecated”

Michel… in your Action Event… where does GRAPHICS come from? A Label has no such property[/quote]

It is the Action event of a button, and it seems the Graphics object does not need to be created. It just works like that :slight_smile:

Thanks Dave.
I’ll try your method. It seems clear.
Anyway it’s a pity that a Label having a Multiline property doesn’t have a number of lines property as well.

Michel
Dave said it correctly. Graphics is deprecated out of Paint event. If you do Ctrl-K (at least on Windows) you get a lot of warnings with all uses of Graphics out of there.

http://documentation.xojo.com/index.php/Graphics

Not a word about deprecation.

Here is the exact sentence in the Paint event :

Directly accessing the Graphics property of the Canvas has been deprecated. Use this event instead.

What that means is that only accessing the Graphics property of the Canvas is deprecated. Not Graphics itself. All pictures have a Graphics property. Printing is done through the Graphics object.

If you care to read Dave’s method, it uses the picture Graphics property. This is a valid use of the Graphics object, just like mine.

[quote=110457:@Ramon SASTRE]Thanks Maximilian,

But my Label is on a window, and Graphics is a property of a Canvas and I have no Canvas. Besides Graphics is deprecated.
Is there no other way?[/quote]
Drawing to the graphics of an object outside any paint event is deprecated.
Create a picture, set the text font and other attributes and measure the text using the graphics of the picture.

which is exactly what I proposed in the code example above.

[quote=110477:@Norman Palardy]Drawing to the graphics of an object outside any paint event is deprecated.
Create a picture, set the text font and other attributes and measure the text using the graphics of the picture.[/quote]

But drawing to the Graphics of a picture is OK, right ? Or am I becoming crazy ?

Yes

Thank you, Dave. That whole graphics deprecation confusion is really annoying :confused:

You used to be able to have code in something like the Window Open event that did

Window1.Graphics.DrawRect …….
THAT is no longer supported

You can access the graphics that is passed to the Paint event - or you could create a picture and draw to it’s Graphics since it persists (thats how you double buffer things for Windows)

[quote=110502:@Norman Palardy]You used to be able to have code in something like the Window Open event that did

Window1.Graphics.DrawRect …….
THAT is no longer supported

You can access the graphics that is passed to the Paint event - or you could create a picture and draw to it’s Graphics since it persists (thats how you double buffer things for Windows)[/quote]

OK. I never tried to draw directly to an object graphics and always used the Paint event or a picture.

I have the impression that Ramon is gravely confused about Graphics and has wrongly concluded that all Graphics are deprecated.