Xamarin Forms Auto Size Font Based on Width of Control

Tony Pitman 81 Reputation points
2022-08-22T17:30:21.507+00:00

I have a cross platform Xamarin.Forms app that runs on iOS and Android. I would like to set the font size for my labels and entry fields to a single value and then if the width of the text exceeds the area given for the control, have the size of the font automatically reduced.

I found a component called Forms9Patch that does this. It works really good on iOS and works on the 2 development Android phones I have. As the text grows, the font size automatically adjusts smaller so the text still fits.

When I started testing with end users, however, we quickly found that if the Font Size setting is larger on their phone, the text doesn't fit anymore. I suspect this is some change in answer since the component was last updated in 2020. I don't know that for sure, but can't think of any other reason.

Can someone either explain that or tell me about some other way to support this? I don't want to have to figure out font sizes myself, or include platform specific coding if that is possible. It would be nice if there was a way to make Forms9Patch work or find another similar component that doesn't have this issue.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2022-08-23T08:40:34.663+00:00

    Hello,

    As far As I known, there is no such an API or official plugin to do it in Xamarin.Froms.

    You could try to use the following ways to implement Auto Size Font manually.

    For Entry, you could use the TextChanged event to control the font size.

    For example, you could use the following code to change the fontsize:

       private void Entry_TextChanged(object sender, TextChangedEventArgs e)  
       {  
           const int max_size = 18;  
           Entry en = sender as Entry;  
           if (e.NewTextValue.Length * en.FontSize > en.Width)  
           {  
               en.FontSize--;  
           }  
           if (e.OldTextValue != null)   
           {  
               if (e.NewTextValue.Length < e.OldTextValue.Length & (e.NewTextValue.Length * (en.FontSize + 1)) < en.Width & en.FontSize < max_size)  
               {  
                   en.FontSize++;  
               }  
           }  
       }  
    

    For Label, you could use Custom Renderer to invoke the native API to control the font size:

    On Android:

       protected override void OnElementChanged(ElementChangedEventArgs<Label> e)  
       {  
           base.OnElementChanged(e);  
           Control.SetAutoSizeTextTypeWithDefaults(AutoSizeTextType.Uniform);  
       }  
    

    On iOS:

       protected override void OnElementChanged(ElementChangedEventArgs<Label> e)  
       {  
           base.OnElementChanged(e);  
         
           var label = Control as UILabel;  
           if (label != null)  
           {  
               label.AdjustsFontSizeToFitWidth = true;  
           }  
       }  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.