首页 文章

导航栏 Headers 字体大小

提问于
浏览
13

我需要在我的iPhone应用程序中更改一个视图控制器的导航栏 Headers 文本的大小 . 我正在使用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]];
}

但是这仅适用于tabBarItem .

5 回答

  • 25

    您需要获取导航栏属性并使用 @property(nonatomic, copy) NSDictionary *titleTextAttributes .

    有关详细信息,请参阅UINavigationBar Class ReferenceCustomizing UINavigationBar section .

    更好地理解您的问题:您拥有什么类型的控制器?

    EDIT

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

    如果您在推送的控制器中访问 navigationController .

    [navigationController.navigationBar setTitleTextAttributes:...];
    

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

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

    你已经得到了它,但你也可以使用以下属性方法 .

    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;
    

    谢谢 .

    适用于iOS 7及更高版本

    For Size:

    NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:17.0],NSFontAttributeName, nil];
    
    self.navigationController.navigationBar.titleTextAttributes = size;
    
  • 10

    从iOS7开始,我总是这样做:

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

    关键字 UITextAttributeFont 已经从iOS7中被压抑了,你应该用 NSFontAttributeName 替换 .

    您可以使用 [self.navigationController.navigationBar setTitleTextAttributes:] 来设置某个navigationController的外观,而 [UINavigationBar appearance] setTitleTextAttributes:] 可以直接用于您的整个应用程序 . (当然您需要将它放在属性位置 . )

  • 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
        }
    

    要么

    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)!];
    
     }
    
  • 4

    我的例子

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

相关问题