FontWidth ( )
Parameters
| None. |
Description
|
Returns the width in pixels of the widest character in the current font. It is a property of the font, not of any particular text - the width of its fattest glyph, usually something like W or a wide symbol. Do not use it to work out how long a string will be on screen: in a proportional font like Arial, "illicit" and "WWWWWWW" are both seven characters and nowhere near the same width. StringWidth measures the actual text and is what you want for centring, right-aligning or fitting text into a box. Where FontWidth earns its keep is column layouts and worst-case sizing: reserving space for a score that might reach seven digits, laying out a fixed-width grid of characters, or sizing a text box so that no plausible string can overflow it. With a monospaced font such as Courier New every character is this wide, so FontWidth()*Len(text$) is exact. It reports on whichever font SetFont selected last, so measure after you switch. See also: StringWidth, FontHeight, StringHeight, SetFont, LoadFont. |
Example
; FontWidth Example ; ----------------- Graphics 640,480,0,2 SetBuffer BackBuffer() ; A small font for the info overlay info_font=LoadFont("Arial",14) size=24 font=LoadFont("Arial",size) While Not KeyDown(1) ; [ / ] shrink or grow the font (reloaded at the new size) If KeyHit(27) And size<36 Then FreeFont font size=size+6 font=LoadFont("Arial",size) End If If KeyHit(26) And size>12 Then FreeFont font size=size-6 font=LoadFont("Arial",size) End If Cls SetFont font ; FontWidth returns the pixel width of the font's WIDEST character - ; a safe fixed cell size, so score digits line up in neat columns w=FontWidth() h=FontHeight() score$="0057200" For i=1 To Len(score$) x=140+(i-1)*w Color 60,90,140 Rect x,200,w,h,False Color 255,255,255 Text x,200,Mid$(score$,i,1) Next SetFont info_font Color 255,255,255 Text 0,0,"[ / ] change font size Esc: exit" Text 0,20,"Arial "+size+"pt: FontWidth() = "+w+" pixel cells" Flip Wend End
Index