更改后退导航栏按钮的字体

48

我希望能够设置我的应用程序导航栏返回按钮的字体,而不需要进行任何太过复杂的操作,并且不会失去按钮的任何其他设计特性(例如,我想保留箭头)。

目前,我在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]];
    }
}

然而,这对于后退按钮没有任何更改,无论应用此代码的是哪个UIViewController(根、当前等)。


自定义UIBarButton项的样式:https://dev59.com/AV4c5IYBdhLWcg3wvsfB#28347428 - Vexy
8个回答

117

要更改所有 UINavigationBars 中出现的所有 UIBarButtonItems 的文本外观,请在 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];

更新:适用于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:

注意:这会更改所有 UIBarButtonItem 的实例,而不仅仅是那些包含在 UINavigationBar 中的实例。

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

Swift3:

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

1
这绝对是一种更简单的方法,适用于所有未来的工具栏按钮项,而不仅仅是返回按钮。 - zachjs
1
运作良好。注意UITextAttributeTextColor等在iOS 7中已被弃用。 - LordParsley
iOS 11怎么样? - Genevios

6

如果有人无法完全让这个工作,这是我是如何做到的,包括在IOS7中返回到根视图控制器:

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;

返回到根视图控制器:

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

也许有人会用到这个。

无法与语言环境一起使用,并且会丢弃预配置的辅助功能提示,这些提示是将应用程序放入 IAS 所需的。除非您想要进行大量额外的工作,否则应始终使用系统提供的按钮。 - user246672

3
以下是所有提到的内容的Swift版本(摘自原始答案):
let customFont = UIFont(name: "customFontName", size: 17.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], forState: .normal)

更多好东西:
https://dev59.com/AV4c5IYBdhLWcg3wvsfB#28347428

2

Swift 3.0+

AppDelegate.swift

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

2

在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

注意:您只需针对导航控制器执行一次此操作。

1
在您的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]; 

1
这并不是一个完整的答案,因为配置的 backButton 没有被使用。 - Mike Pollard
@MikePollard 这个问题是要求更改字体而不是如何使用按钮。如果你想知道如何使用它,应该另外提问。 - Ahmed Z.

0

如果您需要更改标题,可以使用这个:

 UINavigationBar.appearance().titleTextAttributes = [
        NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.heavy)
    ]

如果你需要在你的BarButtonItems中使用它,可以使用以下代码:

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17.0)], for: .normal)

注意:这些代码将被添加到AppDelegate.Swift文件中。

0

如果在iOS 8中使用新的 UISplitViewControllerDelegate来进行分割视图,由于新的displayModeButtonItem工作方式有所不同,上述方法将无法使用。

创建displayModeButtonItem时需要设置字体。假设您正在遵循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];

实际上,经过更多的测试,我认为这不起作用。如果有人知道如何更改displayModeButtonItem上的字体,请告诉我。 - Nick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接