如何更改字体样式或覆盖默认字体样式以在Xamarin.android中完成应用[英] How to change font style or override default font style for complete Application in Xamarin.Android

本文是小编为大家收集整理的关于如何更改字体样式或覆盖默认字体样式以在Xamarin.android中完成应用的处理方法,想解了如何更改字体样式或覆盖默认字体样式以在Xamarin.android中完成应用的问题怎么解决?如何更改字体样式或覆盖默认字体样式以在Xamarin.android中完成应用问题的解决办法?如何更改字体样式或覆盖默认字体样式以在Xamarin.android中完成应用问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我发现许多帖子说要使用FontAttribute="Font_Syle.tff#Font_Style"或通过为每个属性的每个属性制作自定义渲染器,例如label/entry/button/etc&change TypeFace单独地将每个属性的自定义渲染器.

[assembly: ExportRenderer(typeof(Label), typeof(CustomLabel))]
namespace *******.Droid.Renderers
{
    public class CustomLabel : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            var label = (TextView)Control;
            Typeface font = Typeface.CreateFromAsset(Forms.Context.Assets, "Montserrat_Regular.ttf");
            label.Typeface = font;
        }
    }
}

但是,将字体设置为对话框,进度对话框,标题,按编程方式静态创建的列表等存在问题.

推荐答案

最终找到了这样做的解决方案,可以更改完整应用程序的字体.

(Java有很多解决方案,但是C#SOO中没有任何解决方案在此处发布,对以后的某人可能会有所帮助)

代码段

  1. 将其添加到Resource/values/style.xml中.切记将其放在应用程序主题的主要父母STYLE中.
    <item name="android:typeface">serif</item>
  1. 在Droid Project中创建新文件名TypefaceUtil
using Android.Content;
using Android.Graphics;
using Java.Lang;

namespace ***********.Droid
{
    public class TypefaceUtil
    {

        /**
         * Using reflection to override default typeface
         * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
         * @param context to work with assets
         * @param defaultFontNameToOverride for example "monospace"
         * @param customFontFileNameInAssets file name of the font from assets
         */
        public static void overrideFont(Context context, string defaultFontNameToOverride, string customFontFileNameInAssets)
        {
            try
            {
                var typeFace = Class.FromType(typeof(Typeface));
                var customfont = Typeface.CreateFromAsset(context.Assets, customFontFileNameInAssets);
                var font = typeFace.GetDeclaredField(defaultFontNameToOverride);
                font.Accessible = true;
                font.Set(null, customfont);
            }
            catch (System.Exception e)
            {
                var error = e.Message;
            }
        }
    }
}
  1. 在扩展应用程序的主动脉中,调用上述方法.
public class MainApplication : Application
{
   public override void OnCreate()
        {
            base.OnCreate();
            TypefaceUtil.overrideFont(Context, "SERIF", "Montserrat_Regular.ttf"); // font from assets: "assets/fonts/Montserrat_Regular.ttf
   }
}

注意:将您的字体TFF文件放在资源资源文件夹中

仅此而已,字体已设置并准备好:)

本文地址:https://www.itbaoku.cn/post/2284164.html

问题描述

I found many post saying to change font style of every label/entry/button/etc etc statically from xaml files using FontAttribute="Font_Syle.tff#Font_Style" or by making the custom renderer for every of property like label/entry/button/etc & change TypeFace for everyone of it individually.

[assembly: ExportRenderer(typeof(Label), typeof(CustomLabel))]
namespace *******.Droid.Renderers
{
    public class CustomLabel : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            var label = (TextView)Control;
            Typeface font = Typeface.CreateFromAsset(Forms.Context.Assets, "Montserrat_Regular.ttf");
            label.Typeface = font;
        }
    }
}

But there was a problem in setting the font to dialog boxes, progress dialogs, titles, statically created List by programmatically, etc.

推荐答案

Finally found a solution to do so, which can change the font of the complete application.

(There are many solutions for Java but none in C# soo posting it here, it might be helpful for someone in future)

Code Snippet

  1. Add this in Resource/values/style.xml. Remember to place it in the main parent STYLE of your App Theme.
    <item name="android:typeface">serif</item>
  1. Create the new file name TypefaceUtil in Droid project
using Android.Content;
using Android.Graphics;
using Java.Lang;

namespace ***********.Droid
{
    public class TypefaceUtil
    {

        /**
         * Using reflection to override default typeface
         * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
         * @param context to work with assets
         * @param defaultFontNameToOverride for example "monospace"
         * @param customFontFileNameInAssets file name of the font from assets
         */
        public static void overrideFont(Context context, string defaultFontNameToOverride, string customFontFileNameInAssets)
        {
            try
            {
                var typeFace = Class.FromType(typeof(Typeface));
                var customfont = Typeface.CreateFromAsset(context.Assets, customFontFileNameInAssets);
                var font = typeFace.GetDeclaredField(defaultFontNameToOverride);
                font.Accessible = true;
                font.Set(null, customfont);
            }
            catch (System.Exception e)
            {
                var error = e.Message;
            }
        }
    }
}
  1. In the MainActivity which extends Application, call the above-created method.
public class MainApplication : Application
{
   public override void OnCreate()
        {
            base.OnCreate();
            TypefaceUtil.overrideFont(Context, "SERIF", "Montserrat_Regular.ttf"); // font from assets: "assets/fonts/Montserrat_Regular.ttf
   }
}

Note: Place your Font tff file in Asset folder of Resource

That's all, font is all set & ready to go :)

查看更多