Identify label text limits

I’m wanting to figure out when the text in a label is being truncated because it is too long. I see that when this happens a set of periods are displayed on the trailing end, as shown in the picture. I’m wondering if I can reference anything in scripting such as event.source.textTruncated for an example to let me know when the text is too long.

The reason for this is when the text is too long, I’ve got a timer on the template that runs a script on a property change event (timer value), and I manipulate the text in the label so as to make it traverse from one side to another in a continuous loop. However I only want the script to execute if the length of the text is too long to display in the first place, as the text is changing depending on what message it is pulling back from a SQL DB. At the moment I’ve set an arbitrary number such as if len(text) > 20 then enable timer.

Under all of this its basically just a bunch of Java Graphics stuff, meaning you can use the FontMetrics class like this:

lab = self.parent.getComponent("Label 1")
	g = lab.getGraphics()
	
	fm = g.getFontMetrics()
	
	twidth = fm.stringWidth(lab.text)
	
	return str(twidth)

So this would get the length of the text in “Label 1”. In the example below, the labels are 105px wide so the top one is shortened because the actual text width is 108px.

4 Likes

Thank you mrogers,

That worked exactly as needed.

Also how would I then be able to dynamically change the width of the label? So I could change the width of the label dependent on the width of the text.

i.e. label.width = twidth + 5

You can use transform

system.gui.transform(label, newWidth = twidth + 5)
1 Like