Space Member Licensed User Jun 5, 2022 #1 I want to write a game and I noticed that the text output is different in size on different devices. Drawing with: B4X: Copy cnv.DrawText( "Test", 50,200, xui.CreateFont2(Main.ninjaFont,32), 0xffffffff, "LEFT" ) Dim rect As B4XRect rect.Initialize( 45,110,380,210 ) cnv.DrawRect( rect, 0xffffffff, False, 1 ) In my example, the rectangle and the text should always be equal to each other. But they are not... Only Windows and my Android Tablet are the same. What am I doing wrong? Can someone please nudge me in the right direction? Attachments android handy.png 30.3 KB · Views: 673 android tablet.png 12.6 KB · Views: 645 windows.png 8.5 KB · Views: 569
I want to write a game and I noticed that the text output is different in size on different devices. Drawing with: B4X: Copy cnv.DrawText( "Test", 50,200, xui.CreateFont2(Main.ninjaFont,32), 0xffffffff, "LEFT" ) Dim rect As B4XRect rect.Initialize( 45,110,380,210 ) cnv.DrawRect( rect, 0xffffffff, False, 1 ) In my example, the rectangle and the text should always be equal to each other. But they are not... Only Windows and my Android Tablet are the same. What am I doing wrong? Can someone please nudge me in the right direction?
Erel B4X founder Staff member Licensed User Longtime User Jun 6, 2022 #2 You need to use dip units: Code: Copy cnv.DrawText( "Test", 50dip,200dip, xui.CreateFont2(Main.ninjaFont,32), 0xffffffff, "LEFT" ) Dim rect As B4XRect rect.Initialize( 45dip,110dip,380dip,210dip ) cnv.DrawRect( rect, 0xffffffff, False, 1 )
You need to use dip units: Code: Copy cnv.DrawText( "Test", 50dip,200dip, xui.CreateFont2(Main.ninjaFont,32), 0xffffffff, "LEFT" ) Dim rect As B4XRect rect.Initialize( 45dip,110dip,380dip,210dip ) cnv.DrawRect( rect, 0xffffffff, False, 1 )
Space Member Licensed User Jun 6, 2022 #3 Thank you Erel. This works. But is there a way to determine the output of the text in pixels? (Pos and font size) I'm working on a small game and it would be difficult to switch to dip.
Thank you Erel. This works. But is there a way to determine the output of the text in pixels? (Pos and font size) I'm working on a small game and it would be difficult to switch to dip.
Space Member Licensed User Jun 8, 2022 #5 I've now written a function that returns a font with a fixed pixel height. B4X: Copy Sub GetRealHeightFont( fontHeightInPixel As Float ) As B4XFont Dim fontHeight As Float = 50 Dim realHeight As Float = 1000 Do While Abs(realHeight - fontHeightInPixel) > 1 Dim font As B4XFont = xui.CreateDefaultFont( fontHeight ) Dim r As B4XRect = cnv.MeasureText( "Wq", font ) Dim realHeight As Float = r.Bottom-r.Top fontHeight = fontHeight / ( realHeight / fontHeightInPixel ) Loop Return font End Sub I think it's a bit laborious, but it works on my Android test devices. Thanks for the help
I've now written a function that returns a font with a fixed pixel height. B4X: Copy Sub GetRealHeightFont( fontHeightInPixel As Float ) As B4XFont Dim fontHeight As Float = 50 Dim realHeight As Float = 1000 Do While Abs(realHeight - fontHeightInPixel) > 1 Dim font As B4XFont = xui.CreateDefaultFont( fontHeight ) Dim r As B4XRect = cnv.MeasureText( "Wq", font ) Dim realHeight As Float = r.Bottom-r.Top fontHeight = fontHeight / ( realHeight / fontHeightInPixel ) Loop Return font End Sub I think it's a bit laborious, but it works on my Android test devices. Thanks for the help