Auto-scaling label fontsize in Xamarin

2019-08-24 10:06发布

        <Label x:Name="mainLabel" 
           Grid.Row="0" 
           Grid.Column="0" 
           FontSize="50" 
           Grid.ColumnSpan="3"
           Grid.RowSpan="2"
           VerticalOptions="CenterAndExpand" 
           HorizontalOptions="CenterAndExpand" />

I am making a phone dialing interface and i have a label that outputs the numbers that are pressed.

Question: How can I make the font size of the label decreases and increases depending on the numbers typed so that it will fit on the label?

I tried googling all day but all the samples are outdated.

I am using the latest version of Xamarin and android API 27.

1条回答
何必那么认真
2楼-- · 2019-08-24 10:24

You can have a FontSize with a binding property set for the Label.

eg:

<Label x:Name="mainLabel" 
           Grid.Row="0" 
           Grid.Column="0" 
           FontSize= "{Binding LabelFont}"
           Grid.ColumnSpan="3"
           Grid.RowSpan="2"
           VerticalOptions="CenterAndExpand" 
           HorizontalOptions="CenterAndExpand" />

You can have a TextChanged event triggered for the label where you check the length of label text goes above a value change to desired size and also for a length less than a value.

TextChanged event for Label can be implemented as mentioned in this link

查看更多
登录 后发表回答