# Setting Label Font Through Scripting

**URL:** <https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [April 1, 2020, 7:44pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603 "2020-04-01T19:44:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandersen](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dandersen](https://forum.inductiveautomation.com/u/dandersen)\
**Post date:** [April 1, 2020, 7:44pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/1 "2020-04-01T19:44:59Z")

</div>

So I’m trying to set the font of a label when the screen the label on is shown. I have gone around into a few places but it doesn’t seem to work. I currently have this script running on the container that holds the label.

```auto
from java.awt import Font
if event.propertyName == "componentRunning" and event.newValue:
	print event.source.getComponent('bannerText').font
	event.source.getComponent('bannerText').font = Font('Arial', Font.BOLD, 50)
	print event.source.getComponent('bannerText').font

```

The print statements appear to act correctly…

```auto
java.awt.Font[family=Arial,name=Arial,style=bold,size=20]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]

```

---

<div class="post-metadata">

**Author:** ![jpark](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@jpark](https://forum.inductiveautomation.com/u/jpark)\
**Post date:** [April 1, 2020, 7:47pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/2 "2020-04-01T19:47:12Z")

</div>

Why not just set the font property?

---

<div class="post-metadata">

**Author:** ![dandersen](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dandersen](https://forum.inductiveautomation.com/u/dandersen)\
**Post date:** [April 1, 2020, 9:24pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/3 "2020-04-01T21:24:32Z")

</div>

This is a simplified example. We are going to be passing each label into a script to initialize it so they all look the same. Also that way if we need to change it, we can change it in 1 location for every label without having to go to each screen. We are doing the same thing with tables. Each table gets passed to a script that sets up all the aspects so they are all the same.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 2, 2020, 3:23pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/4 "2020-04-02T15:23:54Z")

</div>

In the a project startup script, create a font entry in globals:

```auto
from java.awt import Font
globalFont = Font('Arial', Font.BOLD, 50)
system.util.getGlobals()['globalFont'] = globalFont

```

In the propertyChange script for the label:

```python
# The 'text' property fires every time a screen starts
if event.propertyName == 'text':
	# Get component object and global font
	component = event.source
	globalFont = system.util.getGlobals()['globalFont']
	# set component to global font, if they don't match
	if component.font != globalFont:
		component.font = globalFont
		

```

After you get the desired effects you want, you can put this label into a custom component pallete and use it wherever you wish.

---

<div class="post-metadata">

**Author:** ![dandersen](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dandersen](https://forum.inductiveautomation.com/u/dandersen)\
**Post date:** [April 2, 2020, 6:41pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/5 "2020-04-02T18:41:38Z")

</div>

That doesn’t work either. I can see from the print statements that the font is taking, but the screen doesn’t update. It’s like you need to invoke a repaint of the screen.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 2, 2020, 10:51pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/6 "2020-04-02T22:51:30Z")

</div>

What version are you using? I tested this under 8.0.9. 😕

---

<div class="post-metadata">

**Author:** ![dandersen](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dandersen](https://forum.inductiveautomation.com/u/dandersen)\
**Post date:** [April 2, 2020, 11:13pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/7 "2020-04-02T23:13:54Z")

</div>

8.0.7

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 2, 2020, 11:35pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/8 "2020-04-02T23:35:00Z")

</div>

Are you testing in the designer only? What about a ‘real’ client? I strongly suspect something else is at play here.

---

<div class="post-metadata">

**Author:** ![dandersen](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dandersen](https://forum.inductiveautomation.com/u/dandersen)\
**Post date:** [April 3, 2020, 12:20am UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/9 "2020-04-03T00:20:21Z")

</div>

I had tested it from the stand alone client launcher and from the client launched from the designer and by putting the designer into preview mode. None of them were working. We just so happen to have to reboot the dev system today and it now works in all 3 cases.

And just like that the problem is back. Now it will only work when i put the designer into Preview mode. Launching the client in any way causes it not to work.

This is now on the label component

```auto
if event.propertyName == 'text':
	print event.source.font
	print theme.label.pageTitle()
	event.source.font = theme.label.pageTitle()
	print event.source.font

```

Here is the script “theme.label.pageTitle()”

```auto
from java.awt import Font
def pageTitle():
	return Font('Arial', Font.BOLD, 50)

```

Here is the console output

```auto
java.awt.Font[family=Arial,name=Arial,style=bold,size=23]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]

```

This is the complete console when launched from Client Launcher

```auto
19:41:01.248 [Thread-0] INFO com.inductiveautomation.ignition.client.gateway_interface.GatewayConnectionManager - Updated login state. Logged in? true, Username: admin, Roles: [Administrator], Security Zones: null
Creating frame. extendedState=6, bounds=java.awt.Rectangle[x=0,y=0,width=1918,height=938]
19:41:01.355 [Thread-4] INFO tags.manager.gwinterface - Tag poll rate changed to 250 ms
19:41:01.376 [ClientExecEngine-2] INFO tags.subscriptions - Changing connected quality to 'Good'
19:41:03.159 [Thread-4] INFO com.inductiveautomation.reporting.client.ReportingClientHook - Starting up Reporting Module. Mode: Trial
19:41:03.366 [Thread-4] ERROR Scripting.ScriptManager.OMITTED- Warning: collision at system.util.initialize
19:41:03.753 [AWT-EventQueue-0] INFO vision.App - Starting Up...
java.awt.Font[family=Arial,name=Arial,style=bold,size=23]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]
java.awt.Font[family=Arial,name=Arial,style=bold,size=50]

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 3, 2020, 3:42pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/10 "2020-04-03T15:42:09Z")

</div>

You’ve tried this script on a brand new window, no bindings, other scripts, etc?  
What if you use ‘Dialog’ instead of ‘Arial’? What if you change the script where you’re assigning; use:

```auto
	newFont = theme.label.pageTitle()
        print newFont
	event.source.setFont(newFont)

```

---

<div class="post-metadata">

**Author:** ![dandersen](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dandersen](https://forum.inductiveautomation.com/u/dandersen)\
**Post date:** [April 22, 2020, 10:09pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/11 "2020-04-22T22:09:46Z")

</div>

I tried it using the setFont method and that appears to make it work 100% of the time now. Is there an issue when just setting it equal to a font as I was doing verse using the setFont method? I would have thought they would produce the exact same result.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 22, 2020, 10:15pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/12 "2020-04-22T22:15:42Z")

</div>

They _should_, but it is a little bit of Jython “magic”; Java code style discourages publicly accessible properties, and instead encourages the ‘Bean’ pattern, where there’s a private field and a `getX()`/`setX()` method. Jython does some under-the-hood stuff to turn `getX/setX` into an accessible “property” `x` - so your code, assigning `component.font = something` is _technically_ calling `component.setFont(something)`; or at least, it should be.

It’s not inconceivable that this breaks down somewhere; either because of something about Ignition’s components, or the way we call Jython code, etc.

---

<div class="post-metadata">

**Author:** ![dandersen](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@dandersen](https://forum.inductiveautomation.com/u/dandersen)\
**Post date:** [April 22, 2020, 10:22pm UTC](https://forum.inductiveautomation.com/t/setting-label-font-through-scripting/33603/13 "2020-04-22T22:22:41Z")

</div>

Simple enough. Thanks for the explanation. I figured since the property was exposed that I should use that instead of the get/set idea that I’m used to in Java. If I encounter weird bugs, I’ll know to try the getter and setter first and go from there.
