I'm looking a way to create a render for a label. I need to show some information (for example: the barcode number) I need it to fit on the label reserved space
I want to show the info with the maxim font size has posible to fit on a single line. And I need to auto resize the text if it is necesary
I read a few blogs about the auto resize of the text on Xamarin, but all of the solutions looks complicated and outdated.
I would like to ask if some one has some code or some updated idea to solve this funccionality, thank you
You could try to use textview.SetAutoSizeTextTypeWithDefaults(AutoSizeTextType.Uniform) and uilabel.AdjustsFontSizeToFitWidth = true on Android and ios.
For Android
in your custom renderer:
class AutoFitLabel:LabelRenderer
{
public AutoFitLabel(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
Control.SetAutoSizeTextTypeWithDefaults(AutoSizeTextType.Uniform);
}
}
For ios:
public class AutoFitLabel: LabelRenderer
{
protected override void OnElementChanged (
ElementChangedEventArgs<Label> e)
{
base.OnElementChanged (e);
var label = Control as UILabel;
if (label != null)
{
label.AdjustsFontSizeToFitWidth = true;
}
}
}
and call in your forms xaml:
<Label HeightRequest="80" HorizontalOptions="FillAndExpand" MaxLines="1"
Text="This Label hasset Height to 80">
</Label>