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.