简体   繁体   中英

How do I adjust the title text font size for my UITabBarController?

I am using a UITabBarController but I only have 2 tabs. Instead of using icons I just want to use text. I created a bar item title but the text is very small and I'm having a hard time modifying it. How can I make a bar item just text that fits the section reasonably?

I've tried to change the font size programmatically but I'm fairly new to swift and am struggling.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        UITabBar.appearance().tintColor = .white
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)

        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.lightGray], for: .normal)


        return true
    }
}

The UITextAttributeFont was deprecated in iOS 7. You should use the NS variant instead:

import UIKit

let appearance = UITabBarItem.appearance()
let attributes = [NSAttributedStringKey.font:UIFont(name: "Your font style", size: 20)]
appearance.setTitleTextAttributes(attributes as [NSAttributedStringKey : Any], for: .normal)

For each of the UIViewController in UITabBarController , call setTitleTextAttributes(_:for:) on controller's tabBarItem in viewDidLoad() , ie

class VC1: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 20.0, weight: .regular)], for: .normal)

    }
}

class VC2: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 20.0, weight: .regular)], for: .normal)
    }
}

Edit:

Since you've created the controllers of tabBarController in the storyboard , here is how you can get that working.

Subclass UITabBarController and set the titleTextAttributes of its viewControllers`` tabBarItem in viewDidLoad()`, ie

class TabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.viewControllers?.forEach({
            $0.tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 20.0, weight: .regular)], for: .normal)
        })
    }
}

Now set TabBarController as class of UITabBarController in storyboard .

I hope this will resolve your issue.

UITabBarItem.appearance()。setTitleTextAttributes([NSAttributedString.Key.font:UIFont(name:“ FontName”,size:10)!],用于:.normal)UITabBarItem.appearance()。setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name:“ FontName”,size:10)!],代表:.selected)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM