How to change font style or override default font style for complete Application in Xamarin.Android

南楼画角 提交于 2021-01-29 13:47:51

问题


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.


回答1:


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 :)



来源:https://stackoverflow.com/questions/63706205/how-to-change-font-style-or-override-default-font-style-for-complete-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!