Page 1 of 1
Font Properties Handler
Posted: Sun Dec 13, 2020 4:57 pm
by cmhjon
Hi all,
I am working on an app that creates a lot of text fields each with several different fonts properties that need set. Although I have all the properties coded, it's like 9 lines of code to set all the text properties for each text field. Although this works fine and there's nothing wrong with it the way it is, I would like to learn how to write a separate handler that would set all the font properties so that the code in the primary mouseUp can be consolidated to one line of code to set all font properties for the text field at hand.
I know this is possible, I'm just not sure how to write it.
Any help is appreciated. Thank you,
Jon

Re: Font Properties Handler
Posted: Sun Dec 13, 2020 8:57 pm
by richmond62
I know this is possible
On what basis?
Re: Font Properties Handler
Posted: Sun Dec 13, 2020 9:28 pm
by cmhjon
Nevermind. I think I figured it out.
Thanks,
Jon
Re: Font Properties Handler
Posted: Sun Dec 13, 2020 9:28 pm
by FourthWorld
Show us what you have and we'll show you how to improve it.
Re: Font Properties Handler
Posted: Sun Dec 13, 2020 11:06 pm
by dunbarx
Hi.
How did you do it?
My first take was to create a single large custom property, or perhaps a field, to store all the properties of all the fields in one place. Something like:
field A, Helvetica,16,bold
field B, Geneva,12,plain
etc.
Then a single card script would be perhaps, assuming a field with the data (pseudo again):
Code: Select all
on setFontprops tField
get line lineOffset(tField,fld "yourField") of fld "yourField"
set the textFont of fld tField to item 2 of it
set the textSize of fld tField to item 3 of it
set the textStyle of fld tField to item 4 of it
etc,
end setFontProps
Now all the effort is allotted to create that "array" of fields and their properties, but you must do this at least once in any case.
Craig
Re: Font Properties Handler
Posted: Mon Dec 14, 2020 2:04 pm
by cmhjon
Hi all,
Well, I thought I had it but I guess not. Here's what thought was possible:
Code: Select all
on mouseUp
setFontProperties("Tester",24,"Helvetica Neue Condensed",10,center,false,false,5,0)
end mouseUp
on setFontProperties theField,theFont,theFontSize,theFontAlign,theLockText,theOpaque,theMargins,theBorderWidth
set textFont of field theField to theFont
set textSize of field theField to theFontSize
set textAlign of field theField to theFontAlign
set lockText of field theField to theLockText
set opaque of field theField to theOpaque
set margins of field theField to theMargins
set borderWidth of field theField to theBorderWidth
end setFontProperties
But it gives me a "Chunk: source is not a container" error.
Any ideas?
Thanks,
Jon
Re: Font Properties Handler
Posted: Mon Dec 14, 2020 4:08 pm
by dunbarx
Hi.
Two things.
1- You have written a FUNCTION call, but require a COMMAND call with " setFontProperties". So you simply remove the parentheses and that part will be fine.
2- You have misorderd the parameters. Step through the handler and you will see, for example, that you are trying to set the fontSize to "Helvetica..." Parameters are resolved in the order they are passed. So if the second parameter passed is "24", as in your posting, then the handler will place that value into "theFont", which is number two in your handler.
Do you know the difference between function and command "subRoutines" in LC?
Craig
Re: Font Properties Handler
Posted: Mon Dec 14, 2020 6:05 pm
by jacque
Another way is to set the properties of the templateField. If all fields use the same font settings then you only need to do this once before creating the first field. Every field that is created will use the templateField properties.
After a new field is created you'd only need to change its name.
Re: Font Properties Handler
Posted: Mon Dec 14, 2020 6:23 pm
by cmhjon
Hi Craig,
Thank you so much! Yes, removing the parentheses and fixing the order of things has resolved the issue. What a "duh" moment on my part!
To answer your question though, I am not 100% clear on the differences between subroutines and functions.
Thank you,
Jon

Re: Font Properties Handler
Posted: Mon Dec 14, 2020 7:27 pm
by dunbarx
@Jacque.
I think each field has its own properties.
@ Jon.
Correct? In any case what Jacque mentioned is a powerful tool, and you should play with it.
A command handler is like a subRoutine, just what you have now. Execution branches from the "main" handler and continues in the subRoutine, and then returns to the main. The subRoutine does its business, and if you want variables to track from one to the other, set them as script local variables at the very least.
Function handlers (usually) return a value to the main handler:
Code: Select all
on mouseUp
put getRandomChar("ABCDEFG") into fld 1
end mouseUp
function getRandomChar var
return any char of var
end getRandomChar
Now could this have been done in-Line in the main handler? Yes, in this trivial case.
In your post, you definitely need a command handler. Lots of ordinary work is done there, and no returned value is required by the main handler.
You will know when you need a function, though, and feel free to ask for help. In the meantime, practice making some. They are reusable and will greatly streamline your code, which currently, I suspect, does all such labor in the main handler. No project of any substance could possibly be built without them.
Know that one can use a command handler to return a value as well, but that is a sort of miniKludge, and can wait...
Craig