简体   繁体   English

导航栏标题字体大小

[英]Navigation Bar Title Font Size

I need to change the size of the Navigation Bar title text for one view controller in my iPhone app. 我需要为iPhone应用程序中的一个视图控制器更改导航栏标题文本的大小。 I'm using iOS5, and tried the following code: 我正在使用iOS5,并尝试了以下代码:

if ([self.tabBarItem respondsToSelector:@selector(setTitleTextAttributes:)]) {
    NSLog(@"*** Support method(iOS 5): setTitleTextAttributes:");
    [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                [UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont,
                                                [UIColor blackColor], UITextAttributeTextColor,
                                                [UIColor grayColor], UITextAttributeTextShadowColor,
                                                [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
                                                nil]];
}

However this only applies to the tabBarItem. 但是,这仅适用于tabBarItem。

You got it already,but you can also use following attributes methods. 您已经知道了,但是也可以使用以下属性方法。

For Color, 对于颜色,

NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor RedColor],UITextAttributeTextColor, nil];

self.navigationController.navigationBar.titleTextAttributes = attributes;

For Size, 对于尺寸,

NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:17.0],UITextAttributeFont, nil];

self.navigationController.navigationBar.titleTextAttributes = size;

Thanks. 谢谢。

For iOS 7 and above 对于iOS 7及更高版本

For Size: 对于尺寸:

NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:17.0],NSFontAttributeName, nil];

self.navigationController.navigationBar.titleTextAttributes = size;

You need to grab the navigation bar property and the use @property(nonatomic, copy) NSDictionary *titleTextAttributes . 您需要获取导航栏属性,并使用@property(nonatomic, copy) NSDictionary *titleTextAttributes

For further info see UINavigationBar Class Reference and Customizing UINavigationBar section . 有关更多信息,请参见UINavigationBar类参考自定义UINavigationBar部分

To understand better your question: What type of controller do you have? 为了更好地理解您的问题:您拥有哪种类型的控制器?

EDIT 编辑

[self.navigationController.navigationBar setTitleTextAttributes:...];

if you access the navigationController within a pushed controller. 如果您在推送的控制器中访问navigationController

[navigationController.navigationBar setTitleTextAttributes:...];

if navigationController is your current UINavigationController . 如果navigationController是您当前的UINavigationController This could be used when for example if you have the following code: 例如,如果您具有以下代码,则可以使用它:

UINavigationController* navigationController = ...;
[navigationController.navigationBar setTitleTextAttributes:...];

Since iOS7, I always do like following: 从iOS7开始,我一直喜欢以下内容:

[[UINavigationBar appearance] setTitleTextAttributes:
    [NSDictionary dictionaryWithObjectsAndKeys:
        [UIFont fontWithName:@"Helvetica-Bold" size:18.0],NSFontAttributeName,
        nil]];

The Keyword UITextAttributeFont has been depressed from iOS7, you'd supposed to replace with NSFontAttributeName . 从iOS7开始,已按下关键字UITextAttributeFont ,您应该将其替换为NSFontAttributeName

You can use [self.navigationController.navigationBar setTitleTextAttributes:] to set someone navigationController's appearence, while [UINavigationBar appearance] setTitleTextAttributes:] works for your whole App directly.(Of course you need to put it in a property position.) 您可以使用[self.navigationController.navigationBar setTitleTextAttributes:]来设置某人navigationController的外观,而[UINavigationBar appearance] setTitleTextAttributes:]可以直接用于整个应用程序。(当然,您需要将其放置在属性位置。)

Swift 2.0: Swift 2.0:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UINavigationBar.appearance().titleTextAttributes = [
            NSFontAttributeName: UIFont(name: "DINNextLTW04-Regular", size: 20)!
        ]

        return true
    }

Or 要么

 override func viewDidLoad() {
  super.viewDidLoad()

  self.navigationController?.navigationBarHidden =  false
  self.title = "SAMPLE"

//Set Color
  let attributes: AnyObject = [ NSForegroundColorAttributeName: UIColor.redColor()]
  self.navigationController!.navigationBar.titleTextAttributes = attributes as? [String : AnyObject]


//Set Font Size
  self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Arial", size: 37.0)!];

 }

My example 我的例子

guard let sansLightFont = UIFont(name: "OpenSans", size: 20) else { return }
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : sansLightFont]

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

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