【问题标题】:Xamarin change font in XAMLXamarin 在 XAML 中更改字体
【发布时间】:2018-03-07 11:03:59
【问题描述】:

如何更改 Xamarin.Forms XAML 中标签的字体?

预测不适用于 XAML 中的字体系列属性。

我在字体系列中尝试了 2 或 3 种字体,例如“Arial”,但它不起作用。

帮我解决这个问题。

<StackLayout Orientation="Vertical">
        <Label Text="Welcome  !"
            VerticalOptions="Start" 
            HorizontalOptions="StartAndExpand"
            FontFamily="Arial" 
        />


</StackLayout>

【问题讨论】:

标签: xamarin xamarin.forms xamarin.android


【解决方案1】:

存储ttf文件位置

  1. Android:YouNameSpace.Droid/Assets/Poppins-Light.ttf
  2. Ios:YouNameSpace.iOS/Resources/fonts/Poppins-Light.ttf
  3. 对于 iOS 需要在 info.plist 文件中放入下面的行

在 App.xaml 中创建静态资源

<Application.Resources>
<ResourceDictionary> 
         <!-- FontFamily -->
        <OnPlatform x:Key="PoppinsBold"
                    x:TypeArguments="x:String"
                    iOS="Poppins-Bold"
                    Android="Poppins-Bold.ttf#Poppins"/>

        <OnPlatform x:Key="PoppinsLight"
                    x:TypeArguments="x:String"
                    iOS="Poppins-Light"
                    Android="Poppins-Light.ttf#Poppins" />

         <!-- style for Lable -->
         <Style x:Key="PoppinsBoldLabelStyle"
               TargetType="{x:Type Label}">
            <Setter Property="FontFamily"
             Value="{StaticResource PoppinsBold}" />
        </Style>

        <Style x:Key="PoppinsLightLabelStyle"
               TargetType="{x:Type Label}">
            <Setter Property="FontFamily"
             Value="{StaticResource PoppinsLight}" />
        </Style>
</ResourceDictionary>
</Application.Resources>

在 Lable 中使用静态资源

<Label Text="Hello"  Style="{StaticResource PoppinsBoldLabelStyle}"/>

【讨论】:

    【解决方案2】:

    我通过复制 Android 的“Assets”文件夹中的字体解决了这个问题,并将代码更改如下:

     <StackLayout Orientation="Vertical">
    
            <Label Text="Hello Forms with XAML" FontFamily="ariblk.ttf#ariblk"/>
            <Label Text="New Line following the first line " FontFamily="chiller.ttf#chiller" />
    
     </StackLayout>
    

    【讨论】:

    • 字体不支持大写字母
    【解决方案3】:

    请参考thishere是官方demo。

    或者你可以使用Custom renderers


    Here 下载Arial,并放入您的Assets/Fonts 文件夹。

    MyLabel 在您的 PCL 中:

    namespace FontTes
    {
        public class MyLabel:Label
        {
        }
    }
    

    MyLabelRender 在您的 Android 平台上:

    using Android.Content;
    using Android.Graphics;
    using Android.Widget;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    [assembly: ExportRenderer(typeof(FontTes.MyLabel), typeof(FontTes.Droid.MyLabelRender))]
    namespace FontTes.Droid
    {
        class MyLabelRender: LabelRenderer
        {
            public MyLabelRender(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
            {
                base.OnElementChanged(e);
                var label = (TextView)Control;
                label.Typeface = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, "Fonts/arial.ttf");
    
            }
        }
    }
    

    PCL's MainPage.xaml:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:FontTes;assembly=FontTes"
                 x:Class="FontTes.MainPage">
        <StackLayout>
    
        <Label Text="Welcome to Xamarin.Forms!" 
               VerticalOptions="Center" 
               HorizontalOptions="Center"/>
            <local:MyLabel
              Text="Welcome to Xamarin.Forms!" 
               VerticalOptions="Center" 
               HorizontalOptions="Center"/>
        </StackLayout>
    </ContentPage>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2017-02-22
      相关资源
      最近更新 更多