Xamarin中的自动缩放标签字体大小 [英] Auto-scaling label fontsize in Xamarin

查看:296
本文介绍了Xamarin中的自动缩放标签字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        <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.

我正在使用最新版本的Xamarin和android API 27.

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

推荐答案

您可以使用FontSize并为Label设置一个绑定属性.

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

例如:

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

对于标签,您可以触发TextChanged事件,在该事件中,您检查标签文本的长度是否超过了将值更改为所需大小的长度,并且还检查了小于值的长度.

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事件可以按照此链接

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

这篇关于Xamarin中的自动缩放标签字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆