导航栏标题字体大小

我需要改变我的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。

您需要获取导航栏属性,并使用@property(nonatomic, copy) NSDictionary *titleTextAttributes

有关更多信息,请参阅UINavigationBar类参考和自定义UINavigationBar部分 。

为了更好地理解你的问题:你有什么types的控制器?

编辑

 [self.navigationController.navigationBar setTitleTextAttributes:...]; 

如果你访问推控制器内的navigationController

 [navigationController.navigationBar setTitleTextAttributes:...]; 

如果navigationController是您当前的UINavigationController 。 例如,如果您有以下代码,则可以使用这种方法:

 UINavigationController* navigationController = ...; [navigationController.navigationBar setTitleTextAttributes:...]; 

你已经知道了,但是你也可以使用下面的属性方法。

对于颜色,

 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; 

自从iOS7以来,我总是这样做:

 [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:18.0],NSFontAttributeName, nil]]; 

关键字UITextAttributeFont已经从iOS7压低,你应该用NSFontAttributeNamereplace。

您可以使用[self.navigationController.navigationBar setTitleTextAttributes:]设置某个navigationController的[UINavigationBar appearance] setTitleTextAttributes:] ,而[UINavigationBar appearance] setTitleTextAttributes:]直接适用于您的整个应用程序(当然,您需要将它放在属性位置)。

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)!]; } 

我的例子

 guard let sansLightFont = UIFont(name: "OpenSans", size: 20) else { return } navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : sansLightFont]