我可以改变我的UINavigationController的字体? - >标题更改字体
更改字体
回答
标题视图可以是任何图。因此,只需创建一个UILabel或更改字体并将该新视图分配给导航项目的title属性的其他内容。
一个例子:
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [FontHelper fontFor:FontTargetForNavigationHeadings];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.navigationItem.title;
// emboss in the same way as the native title
[label setShadowColor:[UIColor darkGrayColor]];
[label setShadowOffset:CGSizeMake(0, -0.5)];
self.navigationItem.titleView = label;
}
作为iOS 5中的可以经由代理外观改变字体。
https://developer.apple.com/documentation/uikit/uiappearance
下面我们来设置所有UINavigationControllers标题字体。
NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:@"Didot" size:16] forKey:NSFontAttributeName];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
要设置后退按钮的字体,这样做:
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
[attributes setValue:[UIFont fontWithName:@"Didot" size:12] forKey:NSFontAttributeName];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
iOS 7弃用了键值:UITextAttributeFont(读取,不用这个)现在使用键:NSFontAttributeName来代替。工程奇迹,使我的应用程序看起来好多了。谢谢! – 2013-10-03 16:30:04
在每个单独的视图控制器中,您还可以在当前导航栏的实例上设置标题文本属性以自定义其外观,如下面的代码片段所示:[self.navigationController.navigationBar setTitleTextAttributes:@ {NSFontAttributeName:[UIFont fontWithName:@“帕拉提诺-粗体”尺寸:28],NSForegroundColorAttributeName:的UIColor whiteColor]}]; – CodePlumber 2015-03-21 16:53:23
为iOS8上的+,你可以使用:
[self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"MyFont" size:18.0f],
NSForegroundColorAttributeName: [UIColor whiteColor]
}];
从@morgancodes答案将设置所有的字体UINavigationController
标题。我已经更新了它的雨燕4:
let attributes = [NSAttributedStringKey.font: UIFont(name: "Menlo", size: 14) as Any]
UINavigationBar.appearance().titleTextAttributes = attributes
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)
- 1. 更改字体
- 2. 更改字体UIBarButtonItem
- 3. 更改TextView字体?
- 4. 更改MathJax字体?
- 5. UINavigationBar更改字体
- 6. 更改字体2013
- 7. TextView字体更改
- 8. 更改ListView字体
- 9. UITableViewActionRow更改字体
- 10. Android字体更改
- 11. MessageBox.Show--字体更改?
- 12. 字体和字体大小更改
- 13. android spinner更改字体字体
- 14. 更改Div字体大小和字体
- 15. 更改`ListPreference`概要的字体字体
- 16. Android更改ListView字体
- 17. DevExpress和C#字体更改
- 18. 更改字体样式
- 19. 更改PagerSlidingTab条的字体?
- 20. 更改字体样式
- 21. 从JComboBox更改Graphics2D字体
- 22. 更改字体重量
- 23. UILabel动画更改字体
- 24. 不能更改NSButton字体
- 25. 更改键盘的字体
- 26. 如何更改gridview字体?
- 27. 更改导航栏字体
- 28. 更改Highcharts字体大小
- 29. 更改Mvx.MvxGridView的字体?
- 30. 角料和更改字体
这是一个伟大的想法,但我现在有一个问题,.. 当我加我的UILabel我对UINavigationController的控件(如Add按钮)不再可见,.. * HM * – 2010-10-30 18:11:14
我固定它,用自己..对不起一个missunderstood;) – 2010-10-30 18:18:15
如果这解决了问题,不要忘记接受的答案 – Erik 2010-10-31 17:15:19