简体   繁体   English

更改后导航栏按钮的字体

[英]Change font of back navigation bar button

I want to be able to set the font of my apps navigation bar back button without doing anything too crazy and without losing any other design characteristics of the button (ie I want to keep the arrow). 我希望能够设置我的应用程序导航栏后退按钮的字体,而不会做任何太疯狂的事情,并且不会丢失按钮的任何其他设计特征(即我想保留箭头)。

Right now I use this in viewDidAppear: to set the font of the normal bar button items. 现在我在viewDidAppear:使用它viewDidAppear:设置普通条形按钮项的字体。

for (NSObject *view in self.navigationController.navigationBar.subviews) {
    if ([view isKindOfClass:[UIButton class]]) {
        [((UIButton*)view).titleLabel setFont:[UIFont 
                                 fontWithName:@"Gill Sans" 
                                         size:14.0]];
    }
}

However this makes no change on the back button, regardless of which UIViewController this code is applied to (root, current, etc.). 但是,这不会对后退按钮进行任何更改,无论该代码应用于哪个UIViewController (root,current等)。

To change the appearance of the text in all UIBarButtonItems appearing in all UINavigationBars , do the following in application:didFinishLaunchingWithOptions: 要更改所有UINavigationBars出现的所有UIBarButtonItems本的外观,请在application:didFinishLaunchingWithOptions:执行以下操作application:didFinishLaunchingWithOptions:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:
    @{UITextAttributeTextColor:[UIColor blackColor],
     UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
     UITextAttributeTextShadowColor:[UIColor whiteColor],
     UITextAttributeFont:[UIFont boldSystemFontOfSize:12.0]
    }
     forState:UIControlStateNormal];

UPDATE: iOS7 friendly version 更新:iOS7友好版

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
 setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[UIColor blackColor],
   NSShadowAttributeName:shadow,
   NSFontAttributeName:[UIFont boldSystemFontOfSize:12.0]
   }
 forState:UIControlStateNormal];

Swift: 迅速:

NOTE: this changes ALL instances of UIBarButtonItem , not just those contained within a UINavigationBar 注意:这会更改UIBarButtonItem所有实例,而不仅仅是UINavigationBar包含的实例

UIBarButtonItem.appearance()
               .setTitleTextAttributes([NSFontAttributeName : ExamplesDefaults.fontWithSize(22)], 
                                       forState: UIControlState.Normal)

Swift3: Swift3:

UIBarButtonItem.appearance()
     .setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FontName-Regular", size: 14.0)!], 
                             for: .normal) 

For anyone that did not fully got this to work, here is how i did it, including popped back to the Root ViewController in IOS7: 对于那些没有完全解决这个问题的人来说,我就是这样做的,包括弹回到IOS7中的Root ViewController:

UIBarButtonItem *backBtn =[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(popToRoot:)];
backBtn.title = @"Back";
[backBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                 [UIFont fontWithName:@"Chalkduster" size:15], NSFontAttributeName,
                                 [UIColor yellowColor], NSForegroundColorAttributeName,
                                 nil]
                       forState:UIControlStateNormal];

self.navigationItem.leftBarButtonItem=backBtn;

popToRoot ViewController: popToRoot ViewController:

- (IBAction)popToRoot:(UIBarButtonItem*)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}

Maybe someone may have use of this. 也许有人可能会使用这个。

Swift version of the all mentioned above (excerpt from the original answer ) : 上面提到的Swift版本(摘自原始答案 ):

let customFont = UIFont(name: "customFontName", size: 17.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], forState: .normal)

More goodies: 更多好东西:
https://stackoverflow.com/a/28347428/469614 https://stackoverflow.com/a/28347428/469614

In Swift3: 在Swift3中:

let font = UIFont(name: "Verdana", size: 10.0)

// Back button
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font!], for: UIControlState.normal)  

// Title in the navigation item
let fontAttributes = [NSFontAttributeName: font]
self.navigationController?.navigationBar.titleTextAttributes = fontAttributes

Note: you only have to do this once for the Navigation controller. 注意:您只需为导航控制器执行一次此操作。

Swift 3.0+ Swift 3.0+

AppDelegate.swift AppDelegate.swift

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "myFont", size: 17.0)!], for: .normal)

Use this instead in your AppDelegate or where the NavigationController is initialized, method available in iOS 5 and above 请在AppDelegate或初始化NavigationController的地方使用此方法,该方法在iOS 5及更高版本中可用

UIBarButtonItem *backbutton =  [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil];  
[backbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                   [UIColor blackColor],UITextAttributeTextColor,[UIFont fontWithName:TEXTFONT size:16.0f],UITextAttributeFont,
                                                   nil] forState:UIControlStateNormal]; 

If you're using the new UISplitViewControllerDelegate for split views in iOS 8, the above methods won't work because the new displayModeButtonItem works a bit differently. 如果您在iOS 8中使用新的UISplitViewControllerDelegate进行拆分视图,则上述方法将无法工作,因为新的displayModeButtonItem工作方式略有不同。

You need to set the font when you're creating the displayModeButtonItem . 您需要在创建displayModeButtonItem时设置字体。 Assuming you're following Apple's templates this is probably in prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender where you would do something like this: 假设您正在关注Apple的模板,这可能是在prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender ,您可以在此处执行以下操作:

// From Apple's Template:
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
[controller setDetailItem:object];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
// New Line to set the font:
[controller.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceSansPro-Regular" size:14]} forState:UIControlStateNormal];

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

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