在 .ttf 文件之间动态更改 Xamarin Forms
Dynamically change between .ttf files Xamarin Forms
我在我的应用程序中使用了自定义字体,需要将字体属性更改为粗体,据我所知这并不是简单地完成的。
我已经设法使用资源字典动态更改默认字体的颜色和属性:
<Color x:Key="LabelColor">White</Color>
<FontAttributes x:Key="LabelFontAtt">None</FontAttributes>
然后使用 MVVM 更改我的 ViewModel 中的颜色和属性:
App.Current.Resources["LabelColor"] = Xamarin.Forms.Color.FromHex("#01bf89");
App.Current.Resources["LabelFontAtt"] = FontAttributes.Bold;
从上一个问题我已经学会了如何将我的自定义字体设置为所有标签的默认字体:
<Style TargetType="Label">
<Setter Property="FontFamily">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>JosefinSlab-Regular.ttf#JosefinSlab-Regular</OnPlatform.Android>
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
我想在 JosefinSlab-Regular.ttf
和 JosefinSlab-Bold.ttf
之间切换,但我没有成功。是否有可能以我已经对属性和颜色所做的变化来完成它,或者应该以其他方式完成吗?
在您当前的设置中,您可以像这样创建第二种样式:
<Style TargetType="Label" x:Key="BoldLabel">
<Setter Property="FontFamily">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>JosefinSlab-Bold.ttf#JosefinSlab-Bold</OnPlatform.Android>
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
然后根据 Style
.
中的密钥换出 Style
,就像换出 TextColor
一样
我显然不知道您要实现什么功能,但从您所说的来看,您似乎在 ViewModel 中加入了 UI 逻辑(例如颜色),这与Xamarin Forms 中应该如何使用 MVVM。如果您想根据 ViewModel 中的数据更改颜色和样式,您还可以查看 Triggers ,它只存在于 UI 端,这样您就可以清楚地分离 ViewModel 逻辑和 UI逻辑。
我在我的应用程序中使用了自定义字体,需要将字体属性更改为粗体,据我所知这并不是简单地完成的。
我已经设法使用资源字典动态更改默认字体的颜色和属性:
<Color x:Key="LabelColor">White</Color>
<FontAttributes x:Key="LabelFontAtt">None</FontAttributes>
然后使用 MVVM 更改我的 ViewModel 中的颜色和属性:
App.Current.Resources["LabelColor"] = Xamarin.Forms.Color.FromHex("#01bf89");
App.Current.Resources["LabelFontAtt"] = FontAttributes.Bold;
从上一个问题我已经学会了如何将我的自定义字体设置为所有标签的默认字体:
<Style TargetType="Label">
<Setter Property="FontFamily">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>JosefinSlab-Regular.ttf#JosefinSlab-Regular</OnPlatform.Android>
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
我想在 JosefinSlab-Regular.ttf
和 JosefinSlab-Bold.ttf
之间切换,但我没有成功。是否有可能以我已经对属性和颜色所做的变化来完成它,或者应该以其他方式完成吗?
在您当前的设置中,您可以像这样创建第二种样式:
<Style TargetType="Label" x:Key="BoldLabel">
<Setter Property="FontFamily">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.Android>JosefinSlab-Bold.ttf#JosefinSlab-Bold</OnPlatform.Android>
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
然后根据 Style
.
Style
,就像换出 TextColor
一样
我显然不知道您要实现什么功能,但从您所说的来看,您似乎在 ViewModel 中加入了 UI 逻辑(例如颜色),这与Xamarin Forms 中应该如何使用 MVVM。如果您想根据 ViewModel 中的数据更改颜色和样式,您还可以查看 Triggers ,它只存在于 UI 端,这样您就可以清楚地分离 ViewModel 逻辑和 UI逻辑。