ios 更改返回导航栏按钮的字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16535507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change font of back navigation bar button
提问by zachjs
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 (i.e. I want to keep the arrow).
我希望能够设置我的应用程序导航栏后退按钮的字体而不会做任何太疯狂的事情并且不会丢失按钮的任何其他设计特征(即我想保留箭头)。
Right now I use this in viewDidAppear:
to set the font of the normal bar button items.
现在我用它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
此代码应用于哪个(根、当前等)。
回答by Mike Pollard
To change the appearance of the text in all UIBarButtonItems
appearing in all UINavigationBars
, do the following in application:didFinishLaunchingWithOptions:
要更改全部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 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:
斯威夫特3:
UIBarButtonItem.appearance()
.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "FontName-Regular", size: 14.0)!],
for: .normal)
回答by PeterK
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 视图控制器:
- (IBAction)popToRoot:(UIBarButtonItem*)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
Maybe someone may have use of this.
也许有人可能会使用这个。
回答by Vexy
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
回答by Vincent
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.
注意:您只需为导航控制器执行此操作一次。
回答by smohn
Swift 3.0+
斯威夫特 3.0+
AppDelegate.swift
AppDelegate.swift
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "myFont", size: 17.0)!], for: .normal)
回答by Ahmed Z.
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];
回答by Nick
If you're using the new UISplitViewControllerDelegatefor 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
. Assuming you're following Apple's templates this is probably in prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
where you would do something like this:
创建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];