B4J Question How to get Font Famyly and style

Discussion in 'B4J Questions' started by tanush62, Aug 10, 2019.

  1. tanush62

    tanush62 New Member

    I know how to set Font (family, style, size)

    Code:
    Sub Process_Globals
        
    Public lbl1, lbl2 As B4XView
        
    Private xfont As B4XFont

    '..........

    End Sub

    Sub Slider2_ValueChange (Value As Double)
        fsize2=Value
        xfont = fx.CreateFont(nfont2, fsize2, 
    TrueFalse)
        lbl2.Font = xfont
    End Sub
    But how to get Family and style (bold, italic) in code from existing label? I want to read them from already defined label. How to extract them from variable like xfont?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    lbl2.Font = xui.CreateFont(fx.CreateFont(...), 20)
    Dim f As Font = lbl2.Font.ToNativeFont
    Log(f.FamilyName)
     
    jimmyF likes this.
  3. tanush62

    tanush62 New Member

    Nice.
    But what about Bold and Italic?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible. However you can use the label font directly and set it to other views.
     
  5. tanush62

    tanush62 New Member

    Found the way. Maybe it will be useful to someone.

    Code:
    Dim af As B4XFont
       
    Dim xaf As String
        
    Dim fbold1, fitalic1 As Boolean
        
    Dim nfont1 As string

        af=label1.Font
        xaf = af    
    'Trick is here, converting B4xfont into string
        Dim xbeg, xend As Int
        xbeg = xaf.IndexOf(
    "family=")
        xend = xaf.IndexOf(
    ", style")
        nfont1=xaf.SubString2(xbeg+
    7,xend)
     
        xbeg = xaf.IndexOf(
    "style=")
        xend = xaf.IndexOf(
    ", size")
     
        xaf=xaf.SubString2(xbeg+
    6,xend)
        fbold1 = xaf.Contains(
    "Bold")
        fitalic1 = xaf.Contains(
    "Italic")
    And it works like a charm.
     
    Last edited: Aug 11, 2019
    xulihang and jimmyF like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice