如何覆盖iOS的Xamarin Forms TabbedPage项目字体? [英] How do I override the Xamarin Forms TabbedPage item fonts for iOS?

查看:117
本文介绍了如何覆盖iOS的Xamarin Forms TabbedPage项目字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要使Xamarin Forms应用程序具有一致的外观,我需要知道如何更改选项卡式页面标签栏图标的字体.使用UITabBarItem.Appearance作为iOS API可能不会产生任何效果.这样做有什么必要?

Wanting to achieve a consistent look for my Xamarin Forms app, I need to know how to change the font for the tabbed page tab bar icons. Using UITabBarItem.Appearance as the iOS API would suggest does not appear to have any effect. What's necessary to do this?

推荐答案

您需要编写一个这样的自定义渲染器,以下代码为您提供了一个提示!它有您要寻找的东西

U need to write a custom renderer like this one , take a clue from below code ! it has what u r seeking

 [assembly: ExportRenderer(typeof(ExtendedTabbedPage), typeof(TabbedPageCustom))]
    namespace App.iOS
    {
        public class TabbedPageCustom : TabbedRenderer
        {
            public TabbedPageCustom ()
        {
            TabBar.TintColor = UIKit.UIColor.White;
            TabBar.BarTintColor = UIKit.UIColor.White;
            TabBar.BackgroundColor = UIKit.UIColor.Red;
        }
        protected override void OnElementChanged (VisualElementChangedEventArgs e)
        {
            base.OnElementChanged (e);

            // Set Text Font for unselected tab states
            UITextAttributes normalTextAttributes = new UITextAttributes();
            normalTextAttributes.Font = UIFont.FromName("ChalkboardSE-Light", 20.0F); // unselected
            normalTextAttributes.TextColor = UIKit.UIColor.Blue;

            UITabBarItem.Appearance.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
        }
        public override UIViewController SelectedViewController {
            get {
                UITextAttributes selectedTextAttributes = new UITextAttributes();
                selectedTextAttributes.Font = UIFont.FromName("ChalkboardSE-Bold", 20.0F); // SELECTED
                if (base.SelectedViewController != null)
                {
                    base.SelectedViewController.TabBarItem.SetTitleTextAttributes(selectedTextAttributes, UIControlState.Normal);
                }
                return base.SelectedViewController;
            }
            set {
                base.SelectedViewController = value;

                foreach (UIViewController viewController in base.ViewControllers)
                {
                    UITextAttributes normalTextAttributes = new UITextAttributes();
                    normalTextAttributes.Font = UIFont.FromName("ChalkboardSE-Light", 20.0F); // unselected
                    normalTextAttributes.TextColor = UIKit.UIColor.Blue;
                    viewController.TabBarItem.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);
                }
            }
        }

    }
}

这篇关于如何覆盖iOS的Xamarin Forms TabbedPage项目字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆