About Monkey 2 › Forums › Monkey 2 Programming Help › DrawText Alignment
This topic contains 3 replies, has 2 voices, and was last updated by
TomToad 1 year, 7 months ago.
Viewing 4 posts - 1 through 4 (of 4 total)
-
AuthorPosts
-
September 14, 2017 at 8:23 am #10464
Hi,
is there a way to align the text (LEFT, CENTER, RIGHT) with DrawText? If not is there a way to get the pixel size of the text to do it by myself?
Thanks!
September 14, 2017 at 10:22 am #10465Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()Local text:="Hello World!"Local rightX:= Width-canvas.Font.TextWidth(text)'left justifycanvas.DrawText(text,0,10)'right justifycanvas.DrawText(text,rightX,30)'center justifycanvas.DrawText(text,rightX/2,60)'orcanvas.DrawText(text,Width/2,90,.5,0 ) '<-use handlesEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndSeptember 14, 2017 at 11:22 am #10466Thanks! So Font.TextWidth is it and handles work as well. Very nice and thank you for that example!
September 14, 2017 at 3:18 pm #10467Didn’t even realize just how flexible using handles on text is until I started thinking about this more. Put text right where you want it without messing with too much math.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )Self.ClearColor = Color.BlackEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()Local rect:= New Rectf(100.0,30.0,Width-100.0,Height-30.0)canvas.Color = Color.Greycanvas.DrawRect(rect)canvas.Color = Color.White'Top leftcanvas.DrawText("0 ",rect.Left,rect.Top,1.0,1.0)canvas.DrawText(" 1",rect.Left,rect.Top,0.0,1.0)canvas.DrawText("2 ",rect.Left,rect.Top,1.0,0.0)canvas.DrawText(" 3",rect.Left,rect.Top,0.0,0.0)'Top Rightcanvas.DrawText("0 ",rect.Right,rect.Top,1.0,1.0)canvas.DrawText(" 1",rect.Right,rect.Top,0.0,1.0)canvas.DrawText("2 ",rect.Right,rect.Top,1.0,0.0)canvas.DrawText(" 3",rect.Right,rect.Top,0.0,0.0)'Bottom Leftcanvas.DrawText("0 ",rect.Left,rect.Bottom,1.0,1.0)canvas.DrawText(" 1",rect.Left,rect.Bottom,0.0,1.0)canvas.DrawText("2 ",rect.Left,rect.Bottom,1.0,0.0)canvas.DrawText(" 3",rect.Left,rect.Bottom,0.0,0.0)'Bottom Rightcanvas.DrawText("0 ",rect.Right,rect.Bottom,1.0,1.0)canvas.DrawText(" 1",rect.Right,rect.Bottom,0.0,1.0)canvas.DrawText("2 ",rect.Right,rect.Bottom,1.0,0.0)canvas.DrawText(" 3",rect.Right,rect.Bottom,0.0,0.0)'Leftcanvas.DrawText("Left1 ",rect.Left,rect.Center.y,1.0,.5)canvas.DrawText(" Left2",rect.Left,rect.Center.y,0.0,.5)'Rightcanvas.DrawText("Right1 ",rect.Right,rect.Center.y,1.0,.5)canvas.DrawText(" Right2",rect.Right,rect.Center.y,0.0,.5)'Topcanvas.DrawText("Top1",rect.Center.x,rect.Top,.5,1.0)canvas.DrawText("Top2",rect.Center.x,rect.Top,.5,0.0)'Bottomcanvas.DrawText("Bottom1",rect.Center.x,rect.Bottom,.5,1.0)canvas.DrawText("Bottom2",rect.Center.x,rect.Bottom,.5,0.0)'Centercanvas.DrawText("Center",rect.Center.x,rect.Center.y,.5,.5)EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndEdit: Thinking this would be good in the Code Library.
-
AuthorPosts
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic.