B4J Question Label Resize According Text Size

Luis Felipe Andrade

Member
Licensed User
Hello, I am trying to figure out a way to resize a label depending on the text size in B4J, the -2 function only works in B4A, the Erel example where the text fits the label is not functional for me because it is the opposite of what desire, also in b4J the .widht property for label is Read Only, I only require that the label be the same width as the text, (required for a long single line text banner where the text can be different size), I would appreciate a short example if possible, please help, thank you in advance
 

Luis Felipe Andrade

Member
Licensed User
Hello Erel, trying to make it work, the code example attached show me:
java.lang.NumberFormatException: For input string: "0xff0000ff"

I just can find Why, thank you in advance.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private fx As JFX
    Dim cvs1 As B4XCanvas
    Dim Fnt As B4XFont
    Private DigitalFontTTF As String = "digi.ttf"
End Sub

Public Sub Initialize

End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Fnt = xui.CreateFont(fx.LoadFont("c:\bizetds", DigitalFontTTF, 24), 24)
End Sub

Private Sub Button1_Click
    Dim text As String

    text="this is some text in the top and left of the screen"
    Dim clr As Int
    Dim r As B4XRect = cvs1.MeasureText(text, Fnt )
    Dim BaseLine As Int = 10 - r.Height / 2 - r.Top
    cvs1.DrawText(text, 0, BaseLine , Fnt, fx.Colors.Red , "CENTER")
End Sub
 
Upvote 0

Luis Felipe Andrade

Member
Licensed User
Thanks to EnriqueGonzalez and Erel, Finally It work for me, I attach the code, maybe someone in the future will need it, The porpuse is expand the label width according the text lenght
expand label width:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private fx As JFX
    Dim cvs1 As B4XCanvas
    Dim Fnt As B4XFont
    Private DigitalFontTTF As String = "arial.ttf"
    Private Pane1 As Pane
    Private TextField1 As TextField
    Private TextField2 As TextField
    Private Label1 As Label
End Sub

Public Sub Initialize
   
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Fnt = xui.CreateFont(fx.LoadFont("c:\phisicalFontPath", DigitalFontTTF, 35), 35)
    ' in designer label1 size must be also 35
End Sub

Private Sub Button1_Click
    Dim text As String
    cvs1.Initialize ( Pane1)
    text="This is an example text to verify the label change width according to text lenght"
    Dim r As B4XRect = cvs1.MeasureText(text, Fnt )
    '''' cvs1.DrawText(text,10,  17 , Fnt, xui.Color_Blue , "LEFT")  no need to draw, just for reference
    ''17 may be different according font size
    Label1.PrefWidth=r.Width+17
    Label1.Text=text
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…