-the rectangle which will contain the text
-the text (duh)
-the font to use
-the foreground color to use for the text
Here's a few notes:
-if the text is ultimately too long to fit in the box, it just gets cut off
-if any word is too long to fit in the width of the box, it just sits on a line by itself and gets truncated
Here's what I can't figure out yet:
-how can I pass an alignment as a param? I tried putting "top left" as a 5th param, and then "fill text tLine at pAlign..." but I keep getting errors
-how to put line breaks into this... still working on it
Code: Select all
public handler mpWrapTextinRect(in pTextBox as Rectangle,in pText as String,in pFont as Font,in pForeC as Paint)
variable tRect as Rectangle
variable tTextW as Number
variable tTextH as Number
variable tMaxRows as Number
variable tWords as List
variable tWord as String
variable tLine as String
variable tTestLine as String
variable tRow as Number
split pText by " " into tWords
set the font of this canvas to pFont
set the paint of this canvas to pForeC
put the height of the layout bounds of text pText on this canvas into tTextH
put the width of pTextBox into tTextW
put the floor of ((the height of pTextBox)/tTextH) into tMaxRows
if tMaxRows = 0 then
return
end if
put 1 into tRow
repeat for each element tWord in tWords
put tLine & tWord into tTestLine
if the width of the layout bounds of text tTestLine on this canvas > tTextW then
fill text tLine at top left of pTextBox on this canvas
add tTextH to the top of pTextBox
put "" into tTestLine
if tRow=tMaxRows then
put "" into tLine
exit repeat
end if
add 1 to tRow
put tWord & " " into tLine
else
put tTestLine & " " into tLine
end if
end repeat
fill text tLine at top left of pTextBox on this canvas
end handler
Code: Select all
public handler OnPaint()
variable tFont as Font
variable tRect as Rectangle
variable tText as String
put "A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools." into tText
set the paint of this canvas to my background paint
put rectangle [0,0,my width,my height] into tRect
fill rectangle path of tRect on this canvas
put font "Arial" at size 18 into tFont
fill rectangle path of tRect on this canvas
mpWrapTextinRect(tRect,tText,tFont,my foreground paint)
end handler