B4J Question [SOLVED] B4XFloatTextField How to Set Custom Font

rwblinn

Well-Known Member
Licensed User
Longtime User
How to change the font of the B4XFloatText field to a custom font loaded from the dirassets folder and created using xui createfont?

Using the sample code below but no font change ... also not with a textfield as B4XView.
Also tried with CSSUtil -font-family = same result.
Note: Labels defined as B4XView are OK.

B4X:
Private DigitalFont As B4XFont
Private DigitalFontTTF As String = "digi.ttf"
DigitalFont = xui.CreateFont(fx.LoadFont(File.DirAssets, DigitalFontTTF, 24), 24)
Label1.Font = DigitalFont
B4XFloatTextField1.TextField.Font = DigitalFont
B4XFloatTextField1.Update

1658308866900.png


Test project attached.
 

Attachments

  • Project.zip
    13.6 KB · Views: 149

Erel

B4X founder
Staff member
Licensed User
Longtime User
1658317076772.png


also not with a textfield as B4XView
It doesn't matter whether a view is declared as B4XView or not. It doesn't affect the behavior.

The case here is a bit more complicated as TextField doesn't have a font property and it must be set with a CSS attribute. Apparently referencing external fonts must be done with a CSS file:
B4X:
@font-face {
    src: url('digi.ttf');
}
.digital_font {
    -fx-font-family: 'Digital-7 Mono';
    -fx-text-fill: red;
    -fx-font-size: 25.00;
}

Code:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    B4XPages.GetNativeParent(Me).Stylesheets.Add(File.GetUri(File.DirAssets, "style.css")) '<---
    Root.LoadLayout("MainPage")
    DigitalFont = xui.CreateFont(fx.LoadFont(File.DirAssets, DigitalFontTTF, 24), 24)
    Label1.Font = DigitalFont
    B4XFloatTextField1.TextField.As(Node).Style = ""
    B4XFloatTextField1.TextField.As(Node).StyleClasses.Add("digital_font")
    TextField1.As(Node).Style = ""
    TextField1.As(Node).StyleClasses.Add("digital_font")
    Button1_Click
End Sub
 
Upvote 0
Top