Text vertical alignment

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Text vertical alignment

Post by Mark » Sun Jun 24, 2012 9:57 pm

I didn't mean to make him feel bad, Jacque. I believe that one line of script would cause others some confusion, though. With your excellent explanation, however, all should be fine now.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

endernafi
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 296
Joined: Wed May 02, 2012 12:23 pm
Contact:

Re: Text vertical alignment

Post by endernafi » Sun Jun 24, 2012 10:06 pm

Jacque and Mark, thanks for your kind comments and constructive criticism.
And about the assumption of the formatted height's necessity to be smaller than the height of field,
a little script can deal with that problem.
As I've said earlier, that snippet of code is rather simple and can be developed and complicated in many ways.
For example:

Code: Select all

# let's be sure that the field includes all of the text
put the rect of field theField into theFieldRect
put the formattedHeight of field theField + \
      the bottomMargin of field theField + \
      item 2 of theFieldRect into item 4 of theFieldRect


# now, let's align the text vertically
set the topMargin of field theField to \
                  (round(((the height of field theField - the formattedHeight of field theField)/2)) + the topMargin of field theField)


Best,

Ender Nafi Elekçioğlu
~... together, we're smarter ...~
__________________________________________

macOS Sierra • LiveCode 7 & xCode 8

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Text vertical alignment

Post by Mark » Sun Jun 24, 2012 11:23 pm

Hi Ender,

I tried your script. If the field is too small to display the entire text and I click a button with your script repeatedly, then the text disappears towards the top. If a field is very large, then the text may move to the bottom of the field. The reason is that the topMargin depends on itself.

The following script seems to work correctly:

Code: Select all

on mouseUp
     put the long id of fld 1 into myFld
     set the fixedLineHeight of myFld to false
     put the formattedheight of line 1 of myFld into myLineHeight
     put (the height of myFld - myLineHeight) / 2 into myVerticalMargins
     set the margins of myFld to 8,myVerticalMargins,8,myVerticalMargins
end mouseUp
(You might need to change the first line of the script for testing). Actually, the text is a few pixels off if both the text and the field are small, but for larger font sizes it seems quite acceptable.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

endernafi
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 296
Joined: Wed May 02, 2012 12:23 pm
Contact:

Re: Text vertical alignment

Post by endernafi » Sun Jun 24, 2012 11:54 pm

yep, it's excellent :)
quite simple, yet effective.
thanks Mark, I needed this...


Best,

Ender Nafi Elekçioğlu
~... together, we're smarter ...~
__________________________________________

macOS Sierra • LiveCode 7 & xCode 8

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Text vertical alignment

Post by snm » Mon Jun 25, 2012 6:03 pm

None of both codes are perfect. Problem not solved :(
Which one is better depends from dimensions of text field and font size.
It depends also from number of text lines.

Marek

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Text vertical alignment

Post by Mark » Mon Jun 25, 2012 6:12 pm

Hi Marek,

I assume you have tried my code. Which problem did you encounter?

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Text vertical alignment

Post by snm » Mon Jun 25, 2012 7:09 pm

Hi Mark,

I checked both codes. As you mentioned "it seems quite acceptable" but not perfect yet.
It is accepted if there is 1 line of text, but not accepted if 2 or more. With your script is stable at wrong position. With Ender's script when you run it once is OK, but if you "refresh" by executing the script more times - each time the lines go step by step to the top of the field.

Marek

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Text vertical alignment

Post by Mark » Mon Jun 25, 2012 7:15 pm

Hi Marek,

Yes, as I wrote before, my script causes the text to be a few pixels off. This is particularly a problem for small texts and small fields. I reckon you could add half of the textheight to the topmargin if the textsize is small.

It is easy to make my script work for multiple lines. Just use the formattedHeight of char 1 to -1 of myFld.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Text vertical alignment

Post by snm » Mon Jun 25, 2012 7:16 pm

Hi Mark and Ender,

I'm sorry for criticism, I appreciate you try to help me. Thanks a lot for that.
But my question was how to declare vertical justify for the field, so text should be always justified independent of how many lines it has - always with equal margins (top and bottom) - like in excel for example.

Marek

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Text vertical alignment

Post by snm » Mon Jun 25, 2012 7:27 pm

Hi Mark,

Yes, changing "line 1" to "char 1 to -1" makes correction for displaying multiline text. It's now acceptable as before for 1-line text was. Even more - It looks OK for bigger sizes of text. But If you check 8 pt size text in 60 px height of fld you can see the line is not in the middle. It looks better then without your script, but it's not perfect yet (as I mentioned before). But I hope "everything is possible in LiveCode".

Looking for Winner :)

Marek

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Text vertical alignment

Post by bn » Mon Jun 25, 2012 9:14 pm

Hi Marek,

try this on text with a fixed lineHeight (i.e. the normal text, not text that changes its textSize from line to line), no leading return, no trailing return. I tried on a field 60 px high and 120 high, textsize from 8 to 14 with 3 lines of text.

Code: Select all

on mouseUp
   put the long id of fld 1 into myFld
   put the effective textSize of myFld into tTextSize
   put round (((tTextSize - 22) * 1/3)) into tKorr
   put the formattedheight of char 1 to - 1 of myFld into myFormattedHeight
   put (the height of myFld - myFormattedHeight) / 2 into myVerticalMargins
   put myVerticalMargins - tKorr into tTopMargin
   put myVerticalMargins + tKorr into tBotMargin
   set the margins of myFld to 8,tTopMargin,8,tBotMargin
end mouseUp
tell us if this works for you.

Kind regards
Bernd

Edit: removed one line from the code that was useless
Last edited by bn on Tue Jun 26, 2012 6:34 pm, edited 1 time in total.

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Text vertical alignment

Post by snm » Tue Jun 26, 2012 5:34 pm

Hi Bernd,

Working perfect, you are great. It's really what I expected. Thanks a lot. :( :shock: :) :D

Marek

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Text vertical alignment

Post by bn » Tue Jun 26, 2012 6:48 pm

Hi Marek,

thanks.

And glad you asked why it works :)
It turns out that Livecode treats the height of the first line a little different than the rest of the lines. The first line is not the full textHeight as the rest of the lines are. It is a little smaller. This depends on the textSize and by experimenting I found that

Code: Select all

put round (((tTextSize - 22) * 1/3)) into tKorr
seems to represent the amount of pixels taken off the textHeight for the first line. This works also if you set the textHeight to a value larger than the automatic textHeight.
It took me 2 days to figure that one out.
Well, since I don't have anything else to do... Shure beats playing chess...

Kind regards
Bernd

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Text vertical alignment

Post by snm » Tue Jun 26, 2012 7:24 pm

Hi Bernd,

It should be to much to ask you how it works, until I understand it (it's only difficult to find the way). I don't want to waste your time, it's better for me if you find some time for me next time I have real problem. But as I understood I made fault and should you ask about how it works. Sorry for that, next time I'll remember to ask to make you pleasure like you made me.

But if you have really nothing to do, I have second question - how to implement it in DataGrid headers with behavior:
- if the label of header is 1 line - adjust it vertically center
- if it doesn't fit in the header's weight, divide the line into 2 or 3 lines and adjust it vertically center

Of course we have ready DG header tall enough to fit all 3 lines of the text.

Thanks a lot once more, best regards,
Marek

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Text vertical alignment

Post by Mark » Tue Jun 26, 2012 8:22 pm

Nice, Bernd. Very clever.

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply