Set Text to the Vertical Center of the field

-hh hh at livecode.org
Sat Apr 2 10:27:22 EDT 2016


Scott R. wrote
> Bernd N and I have been through this -- there's no reliable way to this,
> unless you have a known number of lines, and a fixed point size, line
> height, etc.  Bernd wrote a method that uses the pixel rect of the text
> via snapshot that can work.  My solution is to use buttons since those
> can display centered text, but that may not be an option for some
>  implementations.

The following handler, that doesn't use formattedHeight, works for me.

-- VERTICAL CENTERING OF TEXT   [-hh, Apr 16]
-- This is independent of any text formatting, the only
-- assumption is that ALL of the field's text is visible.

on verticalCenter -- 'me' is the field (may be via 'behavior')
  lock screen; lock messages
  put the traversalOn of me into trv
  set traversalOn of me to true --> for using 'select'
  put the height of me into h
  put cr & cr after me 
  select after char -2 of me 
  put item 2 of the selectedLoc of me into v1
  select before char 1 of me 
  put item 2 of the selectedLoc of me into v0
  delete char -2 to -1 of me 
  set topmargin of me to (h + v0 - v1) div 2
  set traversalOn of me to trv
  unlock screen; unlock messages
end verticalCenter

Note. Of course the same technique, now using vscroll instead
of topmargin, works for centering the line of a highlighted text
portion in a scrolling field (if NOT all text of the field is visible).



--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/Set-Text-to-the-Vertical-Center-of-the-field-tp4702965p4702979.html
Sent from the Revolution - User mailing list archive at Nabble.com.




More information about the use-livecode mailing list