ios 导航栏标题字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9621455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:13:54  来源:igfitidea点击:

Navigation Bar Title Font Size

iphoneobjective-ciosuinavigationcontroller

提问by jcrowson

I need to change the size of the Navigation Bar title text for one view controller in my iPhone app. I'm using iOS5, and tried the following code:

我需要为我的 iPhone 应用程序中的一个视图控制器更改导航栏标题文本的大小。我使用的是 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。

回答by Gajendra K Chauhan

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;

回答by Lorenzo B

You need to grab the navigation bar property and the use @property(nonatomic, copy) NSDictionary *titleTextAttributes.

您需要获取导航栏属性和 use @property(nonatomic, copy) NSDictionary *titleTextAttributes

For further info see UINavigationBar Class Referenceand 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 navigationControllerwithin a pushed controller.

如果您访问navigationController推送控制器内的 。

[navigationController.navigationBar setTitleTextAttributes:...];

if navigationControlleris your current UINavigationController. This could be used when for example if you have the following code:

如果navigationController是您当前的UINavigationController. 例如,如果您有以下代码,则可以使用它:

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

回答by Veight Zhou

Since iOS7, I always do like following:

从 iOS7 开始,我总是喜欢以下内容:

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

The Keyword UITextAttributeFonthas been depressed from iOS7, you'd supposed to replace with NSFontAttributeName.

关键字UITextAttributeFont从 iOS7 中被压低了,你应该用 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:]直接作用于你的整个 App。(当然你需要把它放在一个属性位置。)

回答by A.G

Swift 2.0:

斯威夫特 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)!];

 }

回答by Alexander Khitev

My example

我的例子

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