MeasureStringWidth problems

Trigger

Member
Licensed User
Longtime User
Hi all,

I have some code like this:

B4X:
If TestNo = 1 Then
         lblTestText(i).TextColor = Colors.Black
        lblTestText(i).Typeface = Typeface.CreateNew(Typeface.SERIF, Typeface.STYLE_NORMAL)
       Else
         lblTestText(i).TextColor = Colors.ARGB(255,0,128, 128)
        lblTestText(i).Typeface = Typeface.CreateNew(Typeface.SANS_SERIF, Typeface.STYLE_NORMAL)
       End If
       lblTestText(i).Height = LabelHeight
       lblTestText(i).TextSize = FontSize
        lblTestText(i).Width = Canvas1.MeasureStringWidth(lblTestText(i).Text, lblTestText(i).Typeface, FontSize)
      lblTestText(i).Color = Colors.LightGray

When the font is Serif, the label misses off the last word. When the font is Sans-Serif, the label shows all the words, but the background colour continues to the right of the last word in the label. This suggests to me that in the first instance the measured width is shorter than the actual label width required to contain all the text when the font is Serif, and longer than required for a Serif font.

Any ideas what i am doing wrong?
 

Trigger

Member
Licensed User
Longtime User
I'm now registered!

I have a star! Thanks for sorting that out.

Any ideas what I'm doing wrong with MeasureStringWidth?
 
Upvote 0

Trigger

Member
Licensed User
Longtime User
Thanks for the reply agraham.

I see what you mean. But, I would have thought that since the canvas is initialized to the activity, and the activity has loaded a layout file of the right dimensions (1280 x 800 in this case) that the canvas and the activity would be the same size, and that therefore a measured width on one would be the same on the other.

Is there a way, then, to make my canvas exactly match the activity size?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
the canvas and the activity would be the same size, and that therefore a measured width on one would be the same on the other.
I don't know if that is a valid assumption or not. I suspect not but it needs a comment by someone who understands such things better than I.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Canvas.MeasureStringWidth can be used for this task. It actually doesn't matter how you initialize the canvas in this case. However labels have an internal margin (padding) so you need to add a few pixels.

A simpler solution is to set the label width to -2. This is the value of WRAP_CONTENT. The label will increase its size based on the text.
 
Upvote 0

Trigger

Member
Licensed User
Longtime User
Erel,

Thanks for your answer, that will be helpful, I am sure.

In my case, though, it does not work. Because I am trying to accurately place one label to the right on another, like this:
fred
where "fre" is one label, and "d" is the second label. I do that by setting the location of the "fre" label, then measuring its width so that I can place the "d" label exactly after the first label. If I set the "fre" label width to -2, the "d" label is actually positioned 2 pixels to the left of "fre".
 
Upvote 0
Top