# Identify label text limits

**URL:** <https://forum.inductiveautomation.com/t/identify-label-text-limits/17423>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [March 19, 2018, 9:03pm UTC](https://forum.inductiveautomation.com/t/identify-label-text-limits/17423 "2018-03-19T21:03:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmason](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cmason](https://forum.inductiveautomation.com/u/cmason)\
**Post date:** [March 19, 2018, 9:03pm UTC](https://forum.inductiveautomation.com/t/identify-label-text-limits/17423/1 "2018-03-19T21:03:08Z")

</div>

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.

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/5/5ae22c47a6ef743507b1802abb304d61fbbc220c.PNG)

---

<div class="post-metadata">

**Author:** ![mrogers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mrogers/32/4110_2.png) [@mrogers](https://forum.inductiveautomation.com/u/mrogers)\
**Post date:** [March 19, 2018, 9:31pm UTC](https://forum.inductiveautomation.com/t/identify-label-text-limits/17423/2 "2018-03-19T21:31:40Z")

</div>

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

```auto
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.  
 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/5/5856201dc64df5d1886ae754924e2724aa22d7b9.png)

---

<div class="post-metadata">

**Author:** ![cmason](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cmason](https://forum.inductiveautomation.com/u/cmason)\
**Post date:** [March 20, 2018, 12:21am UTC](https://forum.inductiveautomation.com/t/identify-label-text-limits/17423/3 "2018-03-20T00:21:35Z")

</div>

Thank you mrogers,

That worked exactly as needed.

---

<div class="post-metadata">

**Author:** ![cmason](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@cmason](https://forum.inductiveautomation.com/u/cmason)\
**Post date:** [March 20, 2018, 3:13am UTC](https://forum.inductiveautomation.com/t/identify-label-text-limits/17423/4 "2018-03-20T03:13:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Claus\_Nielsen](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Claus\_Nielsen](https://forum.inductiveautomation.com/u/Claus_Nielsen)\
**Post date:** [March 20, 2018, 7:50am UTC](https://forum.inductiveautomation.com/t/identify-label-text-limits/17423/5 "2018-03-20T07:50:04Z")

</div>

You can use [transform](https://docs.inductiveautomation.com/display/DOC79/system.gui.transform)

```auto
system.gui.transform(label, newWidth = twidth + 5)

```
