I need to create a label by code, by cloning an existing label, which uses Material Icons font.
Here is how it looks: the grey icon is the original label, while the red one is the clone
if I set the system font to anything else than the default (SamsungOne in this example)
the cloned icon font changes to a strange font
Dim new As B4XView = CloneLabelToB4XView(Label1)
Log(new.Font)
Log(new.Text)
B4X:
Public Sub CloneLabelToB4XView(lbl As Label) As B4XView
Dim new As B4XView = lbl
new.Text = lbl.Text
new.Font = xui.CreateFont(lbl.Font, lbl.Font.Size)
new.TextColor = xui.Color_Red
Return new
End Sub
Dim new As B4XView = CloneLabelToB4XView(Label1)
Log(new.Font)
Log(new.Text)
B4X:
Public Sub CloneLabelToB4XView(lbl As Label) As B4XView
Dim new As B4XView = lbl
new.Text = lbl.Text
new.Font = xui.CreateFont(lbl.Font, lbl.Font.Size)
new.TextColor = xui.Color_Red
Return new
End Sub
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private Label1 As Label
Private nativeLabel As Label
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")
nativeLabel.Initialize("")
nativeLabel.Typeface = Label1.Typeface
nativeLabel.Text = Label1.Text
nativeLabel.TextSize = Label1.TextSize
nativeLabel.TextColor = xui.Color_Red
Root.AddView(nativeLabel, 100dip, 100dip, 50dip, 50dip)
End Sub
test
I still don't understand what you want to do.
Here is a simple example of cloning label1 from the design to another from the design and another by programming.
B4X:
Dim TextFont1 As B4XFont = xui.CreateMaterialIcons(48)
Label1.Text = Chr(0xE92B)
Label1.Font = TextFont1
Label1.TextColor = xui.Color_Blue
Label2.Text = Label1.Text
Label2.Font = TextFont1
Label2.TextColor = Label1.TextColor
Dim lbl As Label
lbl.Initialize("")
Dim CloneLabel As B4XView = lbl
CloneLabel.text = Label1.Text
CloneLabel.Font = TextFont1
CloneLabel.TextColor = Label1.TextColor
Root.AddView(CloneLabel, 200dip, 200dip, 50dip, 50dip)
Download the SamsungOne font for free to create great typography.
font.download
use:
B4X:
Public Sub GetFontCustom(FontName As String, FontSize As Int) As B4XFont
'B4i Add to main
'#AppFont: <Font name>.ttf
Dim FontResult As B4XFont
#If B4A
FontResult = xui.CreateFont(Typeface.LoadFromAssets(FontName),FontSize)
#Else If B4J
Dim fx As JFX
FontResult = xui.CreateFont(fx.LoadFont(File.DirAssets, FontName, FontSize), FontSize)
#Else
FontResult = xui.CreateFont(Font.CreateNew2(FontName, FontSize), FontSize)
#End If
Return FontResult
End Sub
explicitly setting the font works fine, but my use case is more general: I'm writing a library that does not know beforehand which font is used for each label.
It may be Material or FontAwesome or anything else: thus the need to clone existing label settings.
Public Sub CloneLabel(TargetLabel As B4XView) As B4XView
Dim OutLabel As B4XView = XUIViewsUtils.CreateLabel
OutLabel.Font = xui.CreateFont2(TargetLabel.Font, TargetLabel.Font.Size)
OutLabel.TextColor = TargetLabel.TextColor
OutLabel.Text = TargetLabel.Text
Return OutLabel
End Sub
or
B4X:
'Create a Label.
Public Sub CreateLabel As B4XView
Dim lbl As Label
lbl.Initialize("")
Return lbl
End Sub
Public Sub CloneLabel(TargetLabel As B4XView) As B4XView
Dim OutLabel As B4XView = XUIViewsUtils.CreateLabel
OutLabel.Font = xui.CreateFont2(TargetLabel.Font, TargetLabel.Font.Size)
OutLabel.TextColor = TargetLabel.TextColor
OutLabel.Text = TargetLabel.Text
Return OutLabel
End Sub
thank you for your continued effort, I was also experimenting with a code quite like yours, but still have different results on my samsung phone.
I added a couple of labels and I see that the issue font happens only with Material and Awesome.
The default font is unaffected
updated proof-of-concept code attached: this happens only when a font other than default is selected in the screen options of the samsung/android control panel
Private Sub CloneLabel(source As B4XView) As B4XView
Dim newlabel1 As B4XView = XUIViewsUtils.CreateLabel
newlabel1.Font = xui.CreateFont2(source.Font, source.Font.Size)
newlabel1.Text = source.Text
newlabel1.TextSize = source.TextSize
newlabel1.TextColor = source.TextColor
Log(source.Font)
Log(newlabel1.Font)
Return newlabel1
End Sub
Private Sub CloneLabel(source As B4XView) As B4XView
Dim newlabel1 As B4XView = XUIViewsUtils.CreateLabel
newlabel1.Font = xui.CreateFont2(source.Font, source.Font.Size)
newlabel1.Text = source.Text
newlabel1.TextSize = source.TextSize
newlabel1.TextColor = source.TextColor
Log(source.Font)
Log(newlabel1.Font)
Return newlabel1
End Sub
I can reproduce the problem.
What is not obvious in this thread is that the OP is talking about using the system tool on the device to change the system font.
When I do what he does the problem appears.
It looks like the B4XFont of the label has info about using the icon file data instead of the TypeFace file data.
These are not the same. Even copying the .font form the original label does not change the new label's font icon info.
What you see is the unicode of the system (not default in this case) TypeFont.