Font Properties Handler

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
cmhjon
Posts: 191
Joined: Tue Aug 04, 2015 11:55 am

Font Properties Handler

Post by cmhjon » Sun Dec 13, 2020 4:57 pm

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 :)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Font Properties Handler

Post by richmond62 » Sun Dec 13, 2020 8:57 pm

I know this is possible
On what basis?

cmhjon
Posts: 191
Joined: Tue Aug 04, 2015 11:55 am

Re: Font Properties Handler

Post by cmhjon » Sun Dec 13, 2020 9:28 pm

Nevermind. I think I figured it out.

Thanks,
Jon

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10048
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Font Properties Handler

Post by FourthWorld » Sun Dec 13, 2020 9:28 pm

Show us what you have and we'll show you how to improve it.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Font Properties Handler

Post by dunbarx » Sun Dec 13, 2020 11:06 pm

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

cmhjon
Posts: 191
Joined: Tue Aug 04, 2015 11:55 am

Re: Font Properties Handler

Post by cmhjon » Mon Dec 14, 2020 2:04 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Font Properties Handler

Post by dunbarx » Mon Dec 14, 2020 4:08 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7391
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Font Properties Handler

Post by jacque » Mon Dec 14, 2020 6:05 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

cmhjon
Posts: 191
Joined: Tue Aug 04, 2015 11:55 am

Re: Font Properties Handler

Post by cmhjon » Mon Dec 14, 2020 6:23 pm

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 :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Font Properties Handler

Post by dunbarx » Mon Dec 14, 2020 7:27 pm

@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. :wink:

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

Post Reply