简体   繁体   English

如何调整UITabBarController的标题文本字体大小?

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

I am using a UITabBarController but I only have 2 tabs. 我正在使用UITabBarController但我只有2个标签。 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: UITextAttributeFont在iOS 7中已弃用。您应该改用NS变体:

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 对于UITabBarController每个UIViewController ,在viewDidLoad()控制器的tabBarItem上调用setTitleTextAttributes(_:for:) ,即

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. 由于您已经在storyboard tabBarController中创建了tabBarControllercontrollers ,因此可以通过以下方法实现该功能。

Subclass UITabBarController and set the titleTextAttributes of its viewControllers`` tabBarItem in viewDidLoad()`, ie 子类化UITabBarControllerin viewDidLoad()中设置其viewControllers`` tabBarItem的titleTextAttributes ,即

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 . 现在设置TabBarController作为classUITabBarControllerstoryboard

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据 navigationItem.title 的设备大小调整字体大小 - how do i adjust font size depending on device size of navigationItem.title 如何根据文本字段的大小调整字体大小? - How can I adjust the font size according to the size of the text field? Swift:我可以使用动态字体大小来调整属性文本吗? - Swift: Can I adjust attributed text with dynamic font size? 如何自动调整单元格内容的字体大小以适合 tableview 宽度? - How do I auto adjust the font size of a cells content to fit the tableview width? 如何在UIButton中调整文本的字体大小以在Swift中以编程方式适应宽度? - How to adjust font size of text in a UIButton to fit the width programmatically in Swift? 如何在Swift 2.2中的Pickerview中更改文本/字体大小? - How do i change the text / font size in pickerview in swift 2.2? 调整UILabel字体大小以适合文本 - Adjust UILabel font size to fit text 调整文本的字体大小以适合 UIButton - Adjust font size of text to fit in UIButton 如何快速将弹出窗口调整为 tableview 中内容的大小? - How do I adjust my popover to the size of the content in my tableview in swift? 自动调整导航项标题字体大小以适应 - Automatically adjust Navigation Item Title font size to fit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM