Android Question Adding Fonts .ttf B4X - B4A, B4I, BJ

developer_123

Active Member
Licensed User
Longtime User
I want to know if there is a way to add an a personal font .ttf file in side designer for B4X, b4I and b4J, or if there is a way using compatible code for the 3 platforms. I need use this font in buttons.text and labels.text.

I appreciate any idea!
 

developer_123

Active Member
Licensed User
Longtime User
Thanks Mike, However, it only appears for the TYPEFACE property in B4A, but not in B4I or B4J. My application is multiplatform and it is essential that it works in all of them.
Ok, I see that in B4J with the instruction fx.LoadFont this is possible and it works for me, and in B4I I followed the guide https://www.b4x.com/android/forum/threads/custom-fonts.46461/#content that in fact, between the file name, the font name, and not having included the spaces (like say in the guide) in the font name when defining it in Create New2, made me make several test errors until I got it. By the way, you need to include the spaces in the name.

It would be very good to unify in a future version as common code, or directly from the designer of the 3 platforms, a single way to configure the custom fonts.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is my code for my B4X app:
B4X:
    #If B4i
    #AppFont: courier-new.ttf
    #End If

    #If B4J
    Dim BF As Font = fx.LoadFont(File.DirAssets,"courier-new.ttf", 36)
    #End If
    #If B4A
    Dim TF As Typeface = Typeface.LoadFromAssets("courier-new.ttf")
    Dim BF As B4XFont = xui.CreateFont(TF, 13)
    #End If
    #If B4i
    Dim TF As Font = Font.CreateNew2("courier-new", 30)
    Dim BF As B4XFont = xui.CreateFont2(TF, 13)
    #End If
    LblTotal.Font = BF
 
Upvote 1

developer_123

Active Member
Licensed User
Longtime User
Aeric, So much thanks.
I did it as you indicate, however for B4A I did it directly in the designer since you can include the font in the designer files and immediately select it in the TypeFace of the view properties.

I will carry out tests for B4A according to the code that you indicate.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
just an update for XUI.
B4X:
    'Add in main APP B4i
    '#AppFont: Courier-New.ttf
    
    Dim FontCourierNew As B4XFont
    
    #If B4A
    FontCourierNew = xui.CreateFont(Typeface.LoadFromAssets("Courier-New.ttf"),62)
    #Else If B4J
    Dim fx As JFX
    FontCourierNew = xui.CreateFont(fx.LoadFont(File.DirAssets,"Courier-New.ttf",62),62)
    #Else
    FontCourierNew = xui.CreateFont(Font.CreateNew2("Courier-New.ttf",62),62)
    #End If
    
    Button1.Font = FontCourierNew
    Button1.TextSize = 18
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…