Custom Font in Tabbar

允我心安 提交于 2019-12-30 01:38:27

问题


Hey,
is there a way to set the tabbar's font to e.g. Chalkboard? I've seen the question for the font size and tried it with font, but the loop wouldn't work out.

If there's no way for a custom font, is it possible to remove all text and make the tabbar's item covering the whole tab? Or can I somehow put a imageView on top of the tabbar and use the tabs "through" that image?

Thanks!


回答1:


I've found myself an amazing and very easy tutorial to achieve exactly what I need. For further reference and for all, that might need that too, here's the link

Part 1

Part 2




回答2:


Put in the AppDelegate.m under:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Here is the code:

    NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UITabBarItem appearance] titleTextAttributesForState:UIControlStateNormal]];
[attributes setValue:[UIFont fontWithName:@"Avenir" size:10] forKey:UITextAttributeFont];
[[UITabBarItem appearance] setTitleTextAttributes:attributes1 forState:UIControlStateNormal];



回答3:


On iOS 13 there is a bug with UITabBarItem.appearance().setTitleTextAttributes method, so we need to do workaround like this:

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center
    let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
                      NSAttributedString.Key.paragraphStyle: paragraphStyle]

    let appearance = UITabBarItem.appearance()
    appearance.setTitleTextAttributes(attributes, for: .normal)

    if #available(iOS 13.0, *) {
        let appearance = UITabBarAppearance()
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = attributes
        appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .blue
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = attributes
        appearance.stackedLayoutAppearance.selected.badgeBackgroundColor = .blue
        tabBar.standardAppearance = appearance
    }

If you only set appearance tabBar.standardAppearance = appearance on iOS 13 you'll still have a bug, but the other one



来源:https://stackoverflow.com/questions/6206069/custom-font-in-tabbar

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