我希望能够设置我的应用程序的导航栏后退按钮的字体而不做任何事情太疯狂而不失按钮的任何其他设计特征(即我想保持箭头)。
现在,我在用这个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
此代码被施加到(根,电流等)。
要更改所有文本的外观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];
UPDATE: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
,而不仅仅是那些包含一个内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)
对于任何人都没有完全得到这个工作,这是我如何做的,包括赶回来根的ViewController在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];
}
也许有人可能利用这一点。
上述(摘自所有提到的斯威夫特版本原来的答案 ):
let customFont = UIFont(name: "customFontName", size: 17.0)!
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], forState: .normal)
更多超值服务:
https://stackoverflow.com/a/28347428/469614
在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
注意:您只需要在导航控制器做一次。
斯威夫特3.0+
AppDelegate.swift
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "myFont", size: 17.0)!], for: .normal)
使用此相反在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];
如果您使用的是新UISplitViewControllerDelegate iOS中8分的观点,上述方法将无法工作,因为新displayModeButtonItem
工作稍有不同。
你需要设置字体,当你创建displayModeButtonItem
。 假设你下面的苹果的模板,这可能是在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];