Vertical centering by script

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
grovecat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 99
Joined: Thu Aug 04, 2011 10:32 am

Vertical centering by script

Post by grovecat » Tue Apr 17, 2012 12:33 am

I need to be able to vertically center text in a wrapping text field after a user has entered the text and clicked the appropriate button. The user can vary the text size before centering. The following script is associated with the button. Field height varies so it is passed via a global variable, and the other two variables are used elsewhere in the app.

global gThumbText, gPoke, gFieldHeight

on mouseUp
put field "TextEntry" into gThumbText
put number of lines in field "TextEntry" into tTextLines
put the effective textHeight of field "TextEntry" into tHeight
put (gFieldHeight/tHeight) into tMaxLines
put (tMaxLines - tTextLines)/2 into tReturns
put trunc(tReturns) into gPoke
repeat with x = 1 to gPoke
put return before field "TextEntry"
end repeat
end mouseUp

The above would work if "number of lines" returned the actual number of lines in the field, but it uses a CR to determine where a line ends. So a sentence that runs over six lines is reported as only one line.

I can't see a way to do what I want, so any help would be appreciated.

TIA
Don

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

Re: Vertical centering by script

Post by dunbarx » Tue Apr 17, 2012 1:15 am

Check out the "formattedText" property.

Used correctly, it will reformat wrapped lines in a field with hard CR's, making the number of visible lines equal to the number of actual lines. You can pre-store the wrapped text in a custom property if you need to restore the original.

Craig Newman

grovecat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 99
Joined: Thu Aug 04, 2011 10:32 am

Re: Vertical centering by script

Post by grovecat » Tue Apr 17, 2012 3:11 am

Thanks Craig

I had looked at the formatted text entry in the dictionary but did not twig how to use it for what I wanted. Now you have encouraged me to take a closer look, I realise it just needs something like:

put the number of lines of formattedtext of the field "myText" into tLines
etc.

Cheers
Don

Post Reply