FAQsHow do I center text in a rectangle?I often find it necessary, especially in printouts, to center a string of text in a rectangle. The really great thing about this routine is that the text is drawn in the current mode. In other words, you may set the text mode (as in TextMode ( _srcOr) ) so that the text comes out transparent. In this way, any background will show through the printed items.
local fn getWdHt ( r as ^rect , @wdPtr& , @htPtr& )
wdPtr&.nil& = r.right - r.left
htPtr&.nil& = r.bottom - r.top
end fn
local fn centerText ( r as ^rect , s as str255)
dim as long @ wd , ht
dim as long xCenter , baseLine
dim as fontInfo fi
fn getWdHt ( r , wd , ht )
GetFontInfo ( fi )
baseLine = ( ( ht ) \\ 2 ) + ( ( fi.ascent + fi.descent ) \\ 2 ) + r.top - fi.descent
xCenter = ( ( wd ) \\ 2 ) - ( fn StringWidth ( s ) \\ 2 ) + r.left
MoveTo ( xCenter , baseLine )
TextMode ( _srcOr)
DrawString ( s )
end fn
The function is called like this:
DIM as rect r
SetRect (r 10 , 10 , 200 , 200 )
fn centerText ( r , "This is centered text." )
|