Hi,
I did this some time ago, feel free to use it. (It may not work with scaling and rotating).
Graphics 640,480,0,0
t$ = "DrawTextEx Example~n~n~nJust some text to show how it works!!!"
Repeat
Cls
x0 = GraphicsWidth()/2
y0 = GraphicsHeight()/2
x1 = MouseX()
y1 = MouseY()
w = Abs(x0-x1)
h = Abs(y0-y1)
x0 = Min(x0,x1)
y0 = Min(y0,y1)
SetColor 255,0,0
DrawRect x0,y0,w,h
SetColor 255,255,255
' Possible align flags are:
' H_ALIGN_LEFT (default)
' H_ALIGN_CENTER
' H_ALIGN_RIGHT
' V_ALIGN_TOP (default)
' V_ALIGN_MIDDLE
' V_ALIGN_BOTTOM
'
' Combine flags using | (binary or)
DrawTextEx t$,x0,y0,w,h,H_ALIGN_CENTER|V_ALIGN_MIDDLE
Flip
Until KeyHit(KEY_ESCAPE)
End
'
' The function
'
Const H_ALIGN_LEFT = 0
Const H_ALIGN_CENTER = 1
Const H_ALIGN_RIGHT = 2
Const V_ALIGN_TOP = 0
Const V_ALIGN_MIDDLE = 4
Const V_ALIGN_BOTTOM = 8
Function DrawTextEx( t$,x#,y#,w#=0,h#=0,align%=0 )
Local textlines:String[1]
Local linecount:Int = 1
textlines[0] = t$
Local fitwidth = True
If w<=0
' No fixed limit, if w isn't set
w = 100000
fitwidth = False
End If
' Try to fit inside x,y,w,h
t$.Replace("~n",Chr(10))
linecount = 1
textlines[0] = ""
Local index = 0
Local oldindex = 0
Local newline = False
While oldindex<t.length-1
newline = False
index = t.length-1
For Local i = oldindex To t.length-1
If t[i] = 10
index = i
newline = True
Exit
ElseIf t[i]<=32
index = i
Exit
EndIf
Next
If newline
If TextWidth(textlines[linecount-1]+t[oldindex..index+1])<w
textlines[linecount-1]:+t[oldindex..index+1]
linecount:+1
textlines=textlines[..linecount]
textlines[linecount-1] = " "
Else
linecount:+1
textlines=textlines[..linecount]
textlines[linecount-1] = " "+t[oldindex..index+1]
EndIf
ElseIf TextWidth(textlines[linecount-1]+t[oldindex..index+1])<w Or textlines[linecount-1].length=0
textlines[linecount-1]:+t[oldindex..index+1]
Else
linecount:+1
textlines=textlines[..linecount]
textlines[linecount-1] = ""
index = oldindex-1
EndIf
oldindex = index+1
Wend
If fitwidth = False
' Reset w to 0
w = 0
EndIf
If align & V_ALIGN_MIDDLE
y = y + h*0.5 - TextHeight(t$)*linecount*.5
ElseIf align & V_ALIGN_BOTTOM
y = y + h - TextHeight(t$)*linecount
EndIf
Local dx#
For t$ = EachIn textlines
dx = x
If align & H_ALIGN_CENTER
dx = x + w*0.5 - TextWidth(t$)*.5
ElseIf align & H_ALIGN_RIGHT
dx = x + w - TextWidth(t$)
EndIf
DrawText t,dx,y
y :+ TextHeight(t$)
Next
End Function