cmhjon wrote: ↑Fri Sep 15, 2023 3:58 pm
To me, if you center text in a text field, it should be CENTERED and if I set the margins to zero, the top of the text should align with the top of the field and not be cut off.
LC is weird like that. It doesn’t behave like what you’d expect from other languages, where you would be right. Margins are used to position the text correctly.
LC only centers text horizontally in the property inspector. As others said above, you need to adjust item 2 of the margins to adjust vertical positioning -
the topMargin is shorthand for this (while you’d normally see just one number, that’s just shorthand for saying that all 4 items of the margins are the same). You can do this manually, but if you want to do it automatically - eg when resizing the field or if it's resized automatically with the stack - it gets more difficult to get it right.
The best solution I've found is courtesy of
Bernd - he very kindly added some code for my
tristate script widget to do just this.
I've now abstracted it into a stack-wide command and I use it
everywhere (including data grid fields - the text stays centered vertically if the row height changes for example).
This command can reside in the stack script or in a library and you just need to pass it
the long id of the field you want centre-align vertically:
Code: Select all
command centerVertical pField // the long id of field
local tFieldY, tFormatRect, tFormatheight, tFormatHalfHeight, tCurrFormatTop, tCenterField_To_TopTextDiff
put item 2 of the loc of pField into tfieldY
put the formattedRect of line 1 to - 1 of pField into tFormatRect
put item 4 of tFormatRect - item 2 of tFormatRect into tFormatHeight
put tFormatHeight div 2 into tFormatHalfHeight
put item 2 of tFormatRect into tCurrFormatTop
put tfieldY - tCurrFormatTop into tCenterField_To_TopTextDiff
set the topMargin of pField to the topMargin of pField + tCenterField_To_TopTextDiff - tFormatHalfHeight
end centerVertical
You don't need to understand it

Just use this in a resizeControl or resizeStack handler:
Code: Select all
centerVertical the long id of field <my field>
This is the best way I’ve found to center text vertically, without have to
manually adjust the topMargin (this command does it for you).
All kudos to
@bn (Bernd) with coming up with this.
HTH
S.
-- Edited to correct silly auto-correct errors and to clarify text