Draw text splitted and centered in textbox

Monkey Forums/Monkey Code/Draw text splitted and centered in textbox

Bladko(Posted 2011) [#1]
Code might useful to create centered and justified text in textbox

Global fontWidth = 6
Global fontHeight = 6

'*******************************
'* Draw each tech of the line separated by NEW LINE sing
'*******************************
Function DrawTextJustify( text$,x#,y#,xalign#=0,yalign#=0 )

	if(text.Length < 1) Return 

	Local lines:String[] = text.Split("~n")	
	Local h# = lines.Length 
	Local h1# = fontHeight + 1
	
	For Local n = 0 to h - 1		
		DrawText(lines[n],x - fontWidth / 2, y - h * h1 / 2  + n * h1 + h1 / 2, xalign, yalign) 	
	Next
		
End

'*******************************
'* Adds NEW LINE signs to string 
'* whenever string is longer then setup width
'*******************************
Function ConvertTextIntoLines:String(text$, width)
	
	if(text.Length < 2) Return text

	Local otr:String[] = text.Split(" ")
	
	Local output$ = ""
	
	width = width - 1
		
	Local ss = 0
	For Local n = 0 to otr.Length - 1
		
		'if we have more then should be then create new line
		if((ss + otr[n].Length) * fontWidth >= width)
			output = output + "~n"
			ss = 0
		End 
				
		'add item to line with space (we remove it before)
		output = output + " " + otr[n]
		
		'sum of current line lenght
		ss += otr[n].Length + 1
	
	Next
	
	Return output

End



OUTPUT