ios 更改 UINavigationController 标题的字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4059650/
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:59:10  来源:igfitidea点击:

Change font of UINavigationController title

iosfontsuinavigationcontrollertitle

提问by Christian 'fuzi' Orgler

Can I change the font of my UINavigationController? --> title

我可以更改 UINavigationController 的字体吗?-->标题

采纳答案by Erik

The title view can be any view. So just create a UILabel or something else where you change the font and assign that new view to the title property of the navigation item.

标题视图可以是任何视图。因此,只需创建一个 UILabel 或其他内容,您可以在其中更改字体并将该新视图分配给导航项的 title 属性。

回答by morgancodes

As of iOS 5 you can change the font via the appearance proxy.

从 iOS 5 开始,您可以通过外观代理更改字体。

https://developer.apple.com/documentation/uikit/uiappearance

https://developer.apple.com/documentation/uikit/uiappearance

The following will set the title font for all UINavigationControllers.

下面将设置所有 UINavigationControllers 的标题字体。

  NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
  [titleBarAttributes setValue:[UIFont fontWithName:@"Didot" size:16] forKey:NSFontAttributeName];
  [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];

To set the font for the back button, do this:

要设置后退按钮的字体,请执行以下操作:

  NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
  [attributes setValue:[UIFont fontWithName:@"Didot" size:12] forKey:NSFontAttributeName];
  [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];

To set the font for the large titles available in iOS 11+, do this:

要为 iOS 11+ 中可用的大标题设置字体,请执行以下操作:

if (@available(iOS 11.0, *)) {
    NSMutableDictionary *largeTitleTextAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] largeTitleTextAttributes]];
    [largeTitleTextAttributes setValue:[UIFont fontWithName:@"Didot" size:32] forKey:NSFontAttributeName];
    [[UINavigationBar appearance] setLargeTitleTextAttributes:largeTitleTextAttributes];
}

回答by Maciej

for iOS8+ you can use:

对于 iOS8+,您可以使用:

[self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"MyFont" size:18.0f],
                                                                   NSForegroundColorAttributeName: [UIColor whiteColor]
                                                                   }];

Swift:

迅速:

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MyFont", size: 18.0)!]

回答by Jonathan Moffatt

An example:

一个例子:

-(void) viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    CGRect frame = CGRectMake(0, 0, 400, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    label.backgroundColor = [UIColor clearColor];
    label.font = [FontHelper fontFor:FontTargetForNavigationHeadings];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = self.navigationItem.title;
    // emboss in the same way as the native title
    [label setShadowColor:[UIColor darkGrayColor]];
    [label setShadowOffset:CGSizeMake(0, -0.5)];
    self.navigationItem.titleView = label;
}

回答by apb

The answer from @morgancodes will set the font for all UINavigationControllertitles. I've updated it for Swift 4:

@morgancodes 的答案将为所有UINavigationController标题设置字体。我已经为 Swift 4 更新了它:

let attributes = [NSAttributedStringKey.font: UIFont(name: "Menlo", size: 14) as Any]
UINavigationBar.appearance().titleTextAttributes = attributes
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)