Label1.Text = CombineTextWithMaterialIcons(Chr(0xE0B0) & " a1 B2 c3")
Sub CombineTextWithMaterialIcons(s As String) As RichString
Dim rs As RichString
rs.Initialize(s)
For i = 0 To rs.Length - 1
Log( Asc(s.CharAt(i)))
If Asc(s.CharAt(i)) > 0xe000 Then
rs.TypefaceCustom(lblMaterialIcons.Typeface, i, i + 1)
End If
Next
Return rs
End Sub
Label1.Text = CombineTextWithMaterialIcons(Chr(0xE0B0) & " a1 B2 c3",2) 'Create icon 2x larger than text
Sub CombineTextWithMaterialIcons(s As String, aIconRelSize As Float) As RichString
Dim rs As RichString
rs.Initialize(s)
For i = 0 To rs.Length - 1
Log( Asc(s.CharAt(i)))
If Asc(s.CharAt(i)) > 0xe000 Then
rs.TypefaceCustom(lblMaterialIcons.Typeface, i, i + 1)
rs.RelativeSize(aIconRelSize, i, i+1)
End If
Next
Return rs
End Sub
What I like about this solution is that you can increase the size of the icon relative to the text size
Sub CombineTextWithMaterialIcons(s As String, aIconRelSize As Float) As RichString
I think you are better off if you use a float for the relative size, like this:
Otherwise, 1.5 will be treated as 1, and if you use a relative size of .75, the icon does not even show up.B4X:Sub CombineTextWithMaterialIcons(s As String, aIconRelSize As Float) As RichString
Label1.Text = CombineTextWithMaterialIcons(Chr(0xEB3C) & " 300 Km " & Chr(0xE55F) & Chr(0xE52F) & Chr(0xE530))
Sub Process_Globals
End Sub
Sub Globals
Private lblMaterialIcons As Label
Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
Label1.Text = CombineTextWithMaterialIcons(Chr(0xEB3C) & " 300 Km " & Chr(0xE55F) & Chr(0xE52F) & Chr(0xE530), 1)
End Sub
Sub CombineTextWithMaterialIcons(s As String, aIconRelSize As Float) As RichString
Dim rs As RichString
rs.Initialize(s)
For i = 0 To rs.Length - 1
Log( Asc(s.CharAt(i)))
If Asc(s.CharAt(i)) > 0xe000 Then
rs.TypefaceCustom(lblMaterialIcons.Typeface, i, i + 1)
rs.RelativeSize(aIconRelSize, i, i+1)
rs.Subscript(i, i + 1)
Dim jo As JavaObject = rs
Dim shift As JavaObject
Dim delta As Int = -6dip
shift.InitializeNewInstance(Application.PackageName & ".main$VerticalAlignedSpan", Array(delta))
jo.RunMethod("setSpan", Array(shift, i, i + 1, 0))
End If
Next
Return rs
End Sub
#if Java
import android.text.style.*;
import android.graphics.Paint;
import android.text.TextPaint;
public static class VerticalAlignedSpan extends MetricAffectingSpan {
int shift;
public VerticalAlignedSpan(int shift) {
this.shift = shift;
}
@Override
public void updateDrawState(TextPaint tp) {
tp.baselineShift += shift;
}
@Override
public void updateMeasureState(TextPaint tp) {
tp.baselineShift += shift;
}
}
#End If
I've only tested it on a single device. I'm not sure whether the shift value should be scaled (with dip units) or not. You need to test it on multiple devices.
I've only tested it on a single device. I'm not sure whether the shift value should be scaled (with dip units) or not. You need to test it on multiple devices.
The overhead is negligible.is it worth the extra overhead added by the Javaobject and Richstring libraries to gain a marginal improvement in the way the label looks?
I've only tested it on a single device. I'm not sure whether the shift value should be scaled (with dip units) or not. You need to test it on multiple devices.
actsenden_combinetextwithmaterialicons (B4A line: 759)
shift.InitializeNewInstance(Application.Package
java.lang.ClassNotFoundException: com$aaa$elist$main$VerticalAlignedSpan
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:288)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:83)
at com.aaa.elist.actsenden._combinetextwithmaterialicons(actsenden.java:1048)
at com.aaa.elist.actsenden._activity_create(actsenden.java:434)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at com.aaa.elist.actsenden.afterFirstLayout(actsenden.java:105)
at com.aaa.elist.actsenden.access$000(actsenden.java:20)
at com.aaa.elist.actsenden$WaitForLayout.run(actsenden.java:83)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5418)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
That this is the wrong thread for your question?What am I missing?