Android Question [ Solved]How to code StyleFont from Truetype font

Theera

Expert
Licensed User
Longtime User
This is my code,please help me code StyleFont is Bold,Italic .
B4X:
Private Sub CreateFont
    Try
        If mFontName = "DEFAULT" Or mFontName = "" Then
            ' ใช้ font เริ่มต้นของระบบ(Using Default Font)
            Select mFontStyle.ToUpperCase
                Case "BOLD"
                    mCurrentFont = xui.CreateDefaultBoldFont(mFontSize)  <==Is there xui.CreateDeaultItalicFont() function?
                Case Else ' NORMAL
                    mCurrentFont = xui.CreateDefaultFont(mFontSize)
            End Select
        Else
            ' ใช้ font ที่กำหนด (Using Custom Font)
            Select mFontStyle.ToUpperCase
                Case "BOLD"
                    'mCurrentFont = xui.CreateFontToBold(xui.CreateFont(mFontName, mFontSize, False, False))
                    Dim tf As Typeface=Typeface.LoadFromAssets(mFontName)       '<== How to code StyleFont be Bold?
                    mCurrentFont=xui.CreateFont(tf,mFontSize)
                Case Else ' NORMAL
                    'mCurrentFont = xui.CreateFont(mFontName, mFontSize, False, False)
                    Dim tf As Typeface=Typeface.LoadFromAssets(mFontName)
                    mCurrentFont=xui.CreateFont(tf,mFontSize)
            End Select
        End If
    Catch
        Log("Error creating font, using default")
        mCurrentFont = xui.CreateDefaultFont(mFontSize)
    End Try
  
    ' อัพเดต font ใน label
    If xLBL.IsInitialized Then
        xLBL.Font = mCurrentFont
    End If
End Sub
 
Solution
in b4j
B4X:
    Dim fnt As B4XFont
    fnt = xui.CreateDefaultBoldFont(24) ' fuente normal para comparar
    lbl.Font = fnt
    lbl.Text = "Texto en cursiva B4J"
    lbl.As(Label).Style = "-fx-font-style: italic;"
1753970134595.png

Theera

Expert
Licensed User
Longtime User
What library you are working on?
My library is some feature be worked, but this feature is not. I have used AI help me.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
You must apply the style after applying the font.
test in B4A
B4X:
    Dim Typeface_ITALIC As Int = 2
    Label1.As(JavaObject).RunMethod("setTypeface", Array(Label1.Typeface, Typeface_ITALIC))
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
in b4j
B4X:
    Dim fnt As B4XFont
    fnt = xui.CreateDefaultBoldFont(24) ' fuente normal para comparar
    lbl.Font = fnt
    lbl.Text = "Texto en cursiva B4J"
    lbl.As(Label).Style = "-fx-font-style: italic;"
1753970134595.png
 
Upvote 0
Solution

d3vc

Member
This is my code,please help me code StyleFont is Bold,Italic .
B4X:
Private Sub CreateFont
    Try
        If mFontName = "DEFAULT" Or mFontName = "" Then
            ' ใช้ font เริ่มต้นของระบบ(Using Default Font)
            Select mFontStyle.ToUpperCase
                Case "BOLD"
                    mCurrentFont = xui.CreateDefaultBoldFont(mFontSize)  <==Is there xui.CreateDeaultItalicFont() function?
                Case Else ' NORMAL
                    mCurrentFont = xui.CreateDefaultFont(mFontSize)
            End Select
        Else
            ' ใช้ font ที่กำหนด (Using Custom Font)
            Select mFontStyle.ToUpperCase
                Case "BOLD"
                    'mCurrentFont = xui.CreateFontToBold(xui.CreateFont(mFontName, mFontSize, False, False))
                    Dim tf As Typeface=Typeface.LoadFromAssets(mFontName)       '<== How to code StyleFont be Bold?
                    mCurrentFont=xui.CreateFont(tf,mFontSize)
                Case Else ' NORMAL
                    'mCurrentFont = xui.CreateFont(mFontName, mFontSize, False, False)
                    Dim tf As Typeface=Typeface.LoadFromAssets(mFontName)
                    mCurrentFont=xui.CreateFont(tf,mFontSize)
            End Select
        End If
    Catch
        Log("Error creating font, using default")
        mCurrentFont = xui.CreateDefaultFont(mFontSize)
    End Try
 
    ' อัพเดต font ใน label
    If xLBL.IsInitialized Then
        xLBL.Font = mCurrentFont
    End If
End Sub
Hi , you can try this https://www.b4x.com/android/forum/t...g-b4a-label-and-edittext-views.74727/#content
good luck
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I attached my function in this example
 
Upvote 0
Top