问题非常简单易懂,但是答案却不是那么简单。
如何更改UINavigationBar
中文本的字体?
问题非常简单易懂,但是答案却不是那么简单。
如何更改UINavigationBar
中文本的字体?
从 iOS 7 开始:
NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
shadow.shadowColor = [UIColor redColor];
[[UINavigationBar appearance] setTitleTextAttributes: @{
NSForegroundColorAttributeName: [UIColor greenColor],
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20.0f],
NSShadowAttributeName: shadow
}];
iOS 5及以上版本:
[[UINavigationBar appearance] setTitleTextAttributes: @{
UITextAttributeTextColor: [UIColor greenColor],
UITextAttributeTextShadowColor: [UIColor redColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]
}];
iOS 5 及之前版本:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;
self.navigationItem.titleView = label;
[label release];
...
[self.navigationItem.titleView sizeToFit];
[label release]; // not needed if you are using ARC
已更新至iOS 7:
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,
[UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
提供者:
http://www.appcoda.com/customize-navigation-status-bar-ios-7/
不确定为什么所有答案都包括阴影。添加操作阴影的代码与更改文本字体无关。这两行代码适用于iOS 8.4和Swift。
let attributesDictionary = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!]
navigationController!.navigationBar.titleTextAttributes = attributesDictionary
titleTextAttributes
存储一个字典,该字典将决定导航栏标题的字体、颜色、大小和其他属性。UINavigationBar
的tintColor
。如果您想要不同的颜色,请设置tintColor
或在属性字典中添加一个NSForegroundColorAttributeName
属性和值。 - NRitH NSShadow *shadow = [NSShadow new];
[shadow setShadowColor: [UIColor clearColor]];
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)];
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"TimeBurner" size:27.0f], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName,nil]];
[self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeFont:[UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>]}];
- Philip007