0

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);
        }
    }
}
Cfun
  • 8,442
  • 4
  • 30
  • 62
ssn
  • 11
  • 3
  • you need to show your code – Jason Sep 29 '20 at 13:56
  • have updated the code snippet. Thanks – ssn Sep 29 '20 at 14:22
  • Can you have a try with [XamarinCommunityToolkit](https://github.com/xamarin/XamarinCommunityToolkit#project-structure)? – nevermore Sep 30 '20 at 07:58
  • @JackHua-MSFT could you please provide more information on how to use this NuGet? I did not find the solution related to my query in the provided link. – ssn Sep 30 '20 at 08:33
  • Install that nuget package and use . Have a look at [this article](http://defuncart.com/blog/tech/2019/02/16/learning-xamarin-07-autosize-label.html). – nevermore Oct 02 '20 at 05:14
  • @JackHua-MSFT : i tried the nuget, it is not working. same issue! – ssn Oct 08 '20 at 06:21
  • Yes, I test just now and find it only not work with the FormattedString in Android. Will update you if I find a solution. – nevermore Oct 08 '20 at 09:10
  • You can also open an issue in Github or open a Xamarin ticket for more help:). – nevermore Oct 13 '20 at 09:41
  • I have already asked this question in xamarin forums as well https://forums.xamarin.com/discussion/185278/autosize-label-with-a-formatted-text-not-working-on-android/p1?new=1 – ssn Oct 14 '20 at 09:39
  • Yes, one workaround is not using FormattedText and then the effects:LabelSizeFontToFit will work:). – nevermore Oct 15 '20 at 08:50

0 Answers0