SubName: MeasureText
Description: I came across this while searching the internet. As it seems useful I thought I'd share it. It uses TextBuilder to measure the size of text to be displayed.
It uses a Data 'Type' Structure to return the Height and Width, you can deal with this however you like.
I can't make any claims as to it's accuracy, although it seems pretty good when I am using it to draw text on a canvas.
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Dependencies: JavaObject
Tags: B4j Measure Text Strings
			
			Description: I came across this while searching the internet. As it seems useful I thought I'd share it. It uses TextBuilder to measure the size of text to be displayed.
It uses a Data 'Type' Structure to return the Height and Width, you can deal with this however you like.
I can't make any claims as to it's accuracy, although it seems pretty good when I am using it to draw text on a canvas.
			
				B4X:
			
		
		
		Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Type TextMetric (Width As Int,Height As Int)
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    Dim TM As TextMetric = MeasureText("Test",fx.DefaultFont(15))
    Log(TM.Height & " " & TM.Width)
    Dim TM As TextMetric = MeasureText("Test" & CRLF & "Test2",fx.DefaultFont(15))
    Log(TM.Height & " " & TM.Width)
End Sub
Sub MeasureText(Text As String,TFont As Font) As TextMetric
    Dim TB,Bounds As JavaObject
    Dim TM As TextMetric
    TB.InitializeStatic("javafx.scene.text.TextBuilder")
    Bounds = TB.RunMethodJO("create",Null).RunMethodJO("text",Array(Text)).RunMethodJO("font",Array(TFont)).RunMethodJO("build",Null).RunMethodJO("getLayoutBounds",Null)
    TM.Width = Bounds.RunMethod("getWidth",Null)
    TM.Height = Bounds.RunMethod("getHeight",Null)
    Return TM
End SubDependencies: JavaObject
Tags: B4j Measure Text Strings
			
				Last edited: 
			
		
	
							 
				 
 
		 
 
		 
 
		 
 
		 
 
		