[บทเรียน,B4XCustomView] การเพิ่มคุณสมบัติ Font,TextSize และTextColor แก่ MyB4XCustomView ในฐานะ Button View

Theera

Expert
Licensed User
Longtime User
ขอขอบคุณ PaulMeuris ,Klaus,Erel ,Steveและ Aeric ที่ให้ความรู้ ณ ที่นี่ด้วย

B4XCustomView Class Module Code: MyB4XCustomView
B4X:
Sub Class_Globals
 '   ::::::::
    Private mFont As B4XFont
    Private mTextSize As Float

End Sub

Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
  '  :::::::
    ' Initialize default font and text size
    mFont = xui.CreateDefaultFont(14)
    mTextSize = 14
    xLBL.Font = mFont

   ' ::::::
End Sub


' Font Property Getter
Public Sub getFont As B4XFont
    Return mFont
End Sub

' Font Property Setter
Public Sub setFont(newFont As B4XFont)
    mFont = newFont
    #If B4J
        If xLBL.IsInitialized Then
            CSSUtils.SetStyleProperty(xLBL,"-fx-font-weight","bold")
           'CSSUtils.SetStyleProperty(xLBL,"-fx-font-family","playpensansthai-extrabold")
            CSSUtils.SetStyleProperty(xLBL, "-fx-font-family", $""${MyFont.ToNativeFont.FamilyName}""$)
        End If
    #Else If B4A
        If xLBL.IsInitialized Then
            xLBL.Font = mFont
        End If
    #Else    'B4i
        If xLBL.IsInitialized Then
           xLBL.Font= Font.CreateNew2(mFont.ToNativeFont.FamilyName,mTextSize)
        End If
    #End If
End Sub

' TextSize Property Getter
Public Sub getTextSize As Float
    Return mTextSize
End Sub

' TextSize Property Setter
Public Sub setTextSize(size As Float)
    mTextSize = size
    If xLBL.IsInitialized Then
        ' Create new font with the same typeface but different size
        #If B4A
            mFont = xui.CreateFont(mFont.ToNativeFont, size)
        #Else If B4J
            'mFont = xui.CreateFont(mFont.ToNativeFont, size)
            CSSUtils.SetStyleProperty(xLBL,"-fx-font-size",size)
        #Else 'B4i
            mFont = xui.CreateFont(mFont.ToNativeFont, size)
        #End If
        xLBL.Font = mFont
    End If
End Sub

' TextColor Property Getter
Public Sub getTextColor As Int
    If xLBL.IsInitialized Then
        Return xLBL.TextColor
    Else
        Return xui.Color_Black
    End If
End Sub

' TextColor Property Setter
Public Sub setTextColor(color As Int)
    If xLBL.IsInitialized Then
        xLBL.TextColor = color
    End If
End Sub

วิธีใช้งาน: MyB4XCustomView1

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private MyB4XCustomView1 As MyB4XCustomView
 
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
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")
    Dim myFont As B4XFont
        #If B4A
        myFont = xui.CreateFont(Typeface.LoadFromAssets("playpensansthai-extrabold.ttf"),62)
        #Else If B4J
        Dim fx As JFX
            myFont = xui.CreateFont(fx.LoadFont(File.DirAssets,"playpensansthai-extrabold.ttf",62),62)
        #Else
            myFont = xui.CreateFont(Font.CreateNew2("playpensansthai-extrabold.ttf",62),62)
        #End If
 
      MyB4XCustomView1.Font=myFont
      MyB4XCustomView1.TextSize=12
      MyB4XCustomView1.TextColor=xui.Color_Green
 
End Sub
ศึกษาเพิ่มเติม



 
Last edited:
Top