重写Xamarin TabbedPage,自定义文字大小和字体

发布时间 2023-08-10 13:08:52作者: 醉梦ai天涯

  在Xamarin项目中如果你使用了TabbedPage,并且有需要更改文字的大小或字体,这个需要自定义实现,下面跟随我们来实现如下:

  1、定义CustomerTabbedPage

  

1 public class CustomerTabbedPage : TabbedPage
2     {
3 
4     }

  2、iOS平台实现代码

[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace Test.iOS.Renderers
{
    public class CustomTabbedPageRenderer : TabbedRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
            UpdateAllTabBarItems();
        }

        /// <summary>
        /// Updates the tab bar item for each page included in the TabbedPage.
        /// </summary>
        private void UpdateAllTabBarItems()
        {
            foreach (var controller in ViewControllers)
            {
                controller.TabBarItem.SetTitleTextAttributes(StandardAttributes, UIControlState.Normal);
            }
        }

        /// <summary>
        /// Stores the UITextAttributes for this class.
        /// </summary>
        /// <value>The standard attributes.</value>
        private static UITextAttributes StandardAttributes { get; } = new UITextAttributes() { Font = UIFont.FromName("ContrafaceText-Bold", 12) };
    }
}

  3、Android平台实现

[assembly: ExportRenderer(typeof(Xamarin.Forms.TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace Test.Droid.Renderers
{
    public class CustomTabbedPageRenderer : TabbedPageRenderer
    {
        BottomNavigationView bottomNavigationView;
        public CustomTabbedPageRenderer(Context context) : base(context)
        {
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TabbedPage> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement == null || e.OldElement != null)
                return;
            if (e.NewElement != null)
            {
                bottomNavigationView = (GetChildAt(0) as RelativeLayout).GetChildAt(1) as BottomNavigationView;
                //Call to change the font
                ChangeFont();
            }
        }

        //Change Tab font
        void ChangeFont()
        {
            Typeface fontFace = Typeface.CreateFromAsset(this.Context.Assets, "ContrafaceText-Bold.ttf");
            if (!(bottomNavigationView.GetChildAt(0) is BottomNavigationMenuView bottomNavMenuView))
                return;

            for (int i = 0; i < bottomNavMenuView.ChildCount; i++)
            {
                var item = bottomNavMenuView.GetChildAt(i) as BottomNavigationItemView;
                var itemTitle = item.GetChildAt(1);

                var smallTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(0));
                var largeTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(1));

                smallTextView.SetTypeface(fontFace, TypefaceStyle.Normal);
                largeTextView.SetTypeface(fontFace, TypefaceStyle.Normal);
            }
        }
    }
}