我需要在iPhone应用中更改一个视图控制器的导航栏标题文本的大小。我正在使用iOS5,并尝试了以下代码:
if ([self.tabBarItem respondsToSelector:@selector(setTitleTextAttributes:)]) {
NSLog(@"*** Support method(iOS 5): setTitleTextAttributes:");
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
nil]];
}
但是这仅适用于tabBarItem。
答案 0 :(得分:25)
您已经拥有它,但您也可以使用以下属性方法。
对于Color,
NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor RedColor],UITextAttributeTextColor, nil];
self.navigationController.navigationBar.titleTextAttributes = attributes;
尺寸,
NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:17.0],UITextAttributeFont, nil];
self.navigationController.navigationBar.titleTextAttributes = size;
感谢。
适用于iOS 7及以上版本
尺寸:
NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:17.0],NSFontAttributeName, nil];
self.navigationController.navigationBar.titleTextAttributes = size;
答案 1 :(得分:14)
您需要获取导航栏属性并使用@property(nonatomic, copy) NSDictionary *titleTextAttributes
。
有关详细信息,请参阅UINavigationBar Class Reference和Customizing UINavigationBar section。
更好地理解你的问题:你有什么类型的控制器?
修改强>
[self.navigationController.navigationBar setTitleTextAttributes:...];
如果您在推送的控制器中访问navigationController
。
[navigationController.navigationBar setTitleTextAttributes:...];
如果navigationController
是您当前的UINavigationController
。例如,如果您有以下代码,则可以使用此选项:
UINavigationController* navigationController = ...;
[navigationController.navigationBar setTitleTextAttributes:...];
答案 2 :(得分:10)
从iOS7开始,我总是这样做:
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:18.0],NSFontAttributeName,
nil]];
关键字UITextAttributeFont
已经从iOS7中被压抑了,你应该用它代替
NSFontAttributeName
。
您可以使用
[self.navigationController.navigationBar setTitleTextAttributes:]
来设置某人navigationController的外观,而[UINavigationBar appearance] setTitleTextAttributes:]
可以直接适用于您的整个应用。(当然,您需要将其置于属性位置。)
答案 3 :(得分:4)
Swift 2.0:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "DINNextLTW04-Regular", size: 20)!
]
return true
}
或者
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = false
self.title = "SAMPLE"
//Set Color
let attributes: AnyObject = [ NSForegroundColorAttributeName: UIColor.redColor()]
self.navigationController!.navigationBar.titleTextAttributes = attributes as? [String : AnyObject]
//Set Font Size
self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Arial", size: 37.0)!];
}
答案 4 :(得分:0)
我的例子
guard let sansLightFont = UIFont(name: "OpenSans", size: 20) else { return }
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : sansLightFont]