[英]how do i adjust font size depending on device size of navigationItem.title
[英]How do I adjust the title text font size for my UITabBarController?
我正在使用UITabBarController
但我只有2个标签。 我只想使用文本,而不是使用图标。 我创建了一个酒吧项目标题,但是文本很小,并且很难修改它。 我怎样才能使一个酒吧项目只是适合该部分的文本?
我试图以编程方式更改字体大小,但我还很新,很快就遇到了麻烦。
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
}
}
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)
对于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)
}
}
编辑:
由于您已经在storyboard
tabBarController
中创建了tabBarController
的controllers
,因此可以通过以下方法实现该功能。
子类化UITabBarController
并in
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)
})
}
}
现在设置TabBarController
作为class
的UITabBarController
在storyboard
。
希望这能解决您的问题。
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.