Vertically aligning text in a field

BNig bernd.niggemann at uni-wh.de
Fri Dec 19 12:32:23 EST 2014


here is a definitive (as opposed to infinitive) calculation that correctly
sets the positon of text vertically in a field

for testing purposes it is ready to be put into a slider

optionally make three graphics to get visual feedback, see below

watch out for line breaks
-------------------------------------------------------------------------------------------------------
-- optionally have visual feedback
-- make 3 horizontal line graphics, "gTop", "gBot", "gCenter"
-- place them near the field

on scrollbarDrag pValue -- tested with values 9 to 36
   lock screen
   put the long id of fld "fTest" into myFld -- change to name of your field
   
   set the textSize of myFld to pValue
   
   CenterTextVertically myFld
   
   if there is a grc "gTop" AND there is a grc "gBot" AND there is a grc
"gCenter" then
      getFeedBack myFld
   end if
   
end scrollbarDrag

on CenterTextVertically myFld
   put the effective textHeight of myFld into tTextHeight
   put item 2 of the loc of myFld into tfieldY
   
   put the formattedRect of line 1 to - 1 of myFld into tFormatRect
   put item 2 of tFormatRect into tCurrFormatTop
   put tfieldY - tCurrFormatTop into tCenterField_To_TopTextDiff
   
   put the number of lines of the text of myFld into tNoOfLines
   put (((tNoOfLInes div 2)) * tTextHeight) + (tNoOfLInes mod 2) *
(tTextHeight div 2) into tOffset
   
   set the topMargin of myFld to the topMargin of myFld +
tCenterField_To_TopTextDiff - tOffset
end CenterTextVertically

on getFeedBack myFld
   put the formattedRect of char 1 to - 1 of myFld into tFormatRect
   put item 2 of the loc of myFld into tfieldY
   set the top of grc "gTop" to item 2 of tFormatRect - 1
   set the top of grc "gBot" to item 4 of tFormatRect - 1
   set the top of grc "gCenter" to tFieldY - 1
end getFeedBack
-------------------------------------------------------------------------------------------------------

Did the code "you came across" not do what you wanted?

Kind regards
Bernd



Terence Heaford wrote
> Is there a definitive calculation that correctly sets the position of text
> vertically in a field?
> 
> I am using LC 6.7.
> 
> I have come across a few ideas like the one below that all appear to use
> an arbitrary offset to the margins, is there a method to calculate this
> accurately?
> 
>    put the effective textSize of fld id tID into tTextSize
>    --   put round (((tTextSize - 22) * 1/3)) into tOffset
>    put round (((tTextSize - 24) * 1/3)) into tOffset
>    put the formattedheight of char 1 to - 1 of fld id tID into
> tFormattedHeight
>    put round((the height of fld id tID - tFormattedHeight) / 2) into
> tVerticalMargins
>    put tVerticalMargins - tOffset into tTopMargin
>    put tVerticalMargins + tOffset into tBottomMargin
>    set the margins of fld id tID to 8,tTopMargin,8,tBottomMargin
> 
> All the best
> Terry





--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/Vertically-aligning-text-in-a-field-tp4686824p4686954.html
Sent from the Revolution - User mailing list archive at Nabble.com.




More information about the use-livecode mailing list