AutoSize Label with a Formatted Text not working on Android

468 views Asked by At

I am trying to implement the autofit option in the label, i.e. reduce the font size of the label to fit in the same line. Tried using effects and custom renderers as well. Works fine on iOS but not on Android. According to this blog the implementation did not work on android because I was using Formatted Text inside Label which is not supported by custom fonts. But still, I could not resolve the issue. Has anyone faced this issue before? Any suggestions would be of great help. Thank you in advance.

Update: adding the code snippet for reference.

on PCL project

<Label> 
    <Label.Effects>
        <effects:AutoSizeLabelEffect />
    </Label.Effects>
    
    <Label.FormattedText>
        <FormattedString>
            <Span   Style="fontfamily1" Text="text1" />
            <Span Style="fontfamily2"   Text="text2" />
        </FormattedString>
    </Label.FormattedText>
</Label>
public class AutoSizeLabelEffect : RoutingEffect
{
    public AutoSizeLabelEffect() : base("XXX.AutoSizeLabelEffect")
    {
    }
}

On Android project -> Effects -> OnAttached method

protected override void OnAttached()
{
    if(Control is TextView textView)
    {
        var effect = Element
                     .Effects
                     .OfType<AutoSizeLabelEffect>()
                     .FirstOrDefault();
        if(effect != null)
        {
            TextViewCompat.SetAutoSizeTextTypeUniformWithConfiguration(
                textView,
                effect.MinTextSize,
                effect.MaxTextSize,
                autoSizeStepGranularity: 2,
                (int)ComplexUnitType.Dip);
        }
    }
}
0

There are 0 answers