Dynamically Changing Font Size using Scripting

Hi all,

I want to change the font size of text in a template(there will be a button in the template), essentially by binding it to a custom property (Boolean type). Where can I script it? and how?

So in short, its changing the font size of text in a button, via scripting, based on value of the custom property.

Thanks

Maybe this will help you?? It’s for a table, but maybe it will carry over. I’ve never tried to change font during runtime, so don’t know one way or another. Font is not bindable, so scripting would be your only choice.

I have tried this as a sample, but no luck…

Font=event.source.font
if (1==1):
Font=(Dialog,Bold,15)
else:
event.source.font=12

Used the avove script, but error is that "Dialog has not been recognised… I guess I am missing some syntax… any suggestions?

Got this code working, under the button script only

from java.awt import Font
button = event.source.parent.getComponent(‘Button’)
button.font = Font(‘Dialog’, 0, 16)

Consider moving your code into a script module, as a function taking a boolean and returning your desired font. Something like this:

from java.awt import Font
def myDynamicFont(trigger):
    return Font('Dialog', 0, 32 if trigger else 16)

Then, on your button’s font property, use a runScript expression binding, like so:

runScript("project.someScript.myDynamicFont", 0, {Root Container.Button.CustomBoolean})
1 Like

Another way to do this if appropriate, would be to use the style customiser binding to set the font. Create a custom property on the button to use as the driving property

4 Likes

How do I put the script or expression on font property?

Sorry this is so late…
But you wouldn’t put this script on the property binding itself, you would put this either in a shared script library, as Phil suggested, or you could add it as a custom function on the component itself (shared library is always preferably as it keeps your scripts in one place and organised).
It’s the runScript command that goes on your font binding