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此代码应用于哪个(根、当前等)。

4

8 回答 8

116

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

迅速:

注意:这会更改 的所有实例UIBarButtonItem,而不仅仅是包含在 aUINavigationBar

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

斯威夫特3:

UIBarButtonItem.appearance()
     .setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FontName-Regular", size: 14.0)!], 
                             for: .normal) 
于 2013-05-14T08:40:03.423 回答
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;

popToRoot 视图控制器:

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

也许有人可能会使用这个。

于 2013-10-16T15:02:51.457 回答
4

上述所有内容的 Swift 版本(摘自原始答案):

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

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

于 2015-02-05T15:13:52.163 回答
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

注意:您只需为导航控制器执行一次。

于 2017-01-26T15:09:08.573 回答
2

斯威夫特 3.0+

AppDelegate.swift

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "myFont", size: 17.0)!], for: .normal)
于 2018-01-30T20:07:17.363 回答
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]; 
于 2013-05-14T05:09:16.037 回答
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];
于 2014-11-09T06:49:22.653 回答
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.SwiftFile

于 2021-05-31T13:45:32.317 回答