【问题标题】:How to set navigationbar font and colors如何设置导航栏字体和颜色
【发布时间】:2015-01-05 16:40:48
【问题描述】:

我正在尝试设置导航栏的颜色。我在 plist 文件中添加了我的字体并将其添加为资源,当使用 UILabel 时,我可以在情节提要中选择它作为字体。所以字体似乎在那里。

我已经在应用委托中尝试过这个:

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:myLightBrownColor, NSForegroundColorAttributeName,[UIFont fontWithName:@"Pacifico" size:17.0f], NSFontAttributeName,
                                  myShadow, NSShadowAttributeName, nil];
[navigationBarAppearance setTitleTextAttributes:textTitleOptions];

这直接在视图控制器中:

NSShadow *myShadow = [MRStyleController getMyShadow];
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[MRStyleController getLightBrownColor], NSForegroundColorAttributeName,[UIFont fontWithName:@"Pacifico" size:17.0f], NSFontAttributeName,
                                  myShadow, NSShadowAttributeName, nil];
self.navigationController.navigationBar.titleTextAttributes = textTitleOptions;

但是,这些都没有改变应用导航栏标题的字体和颜色。

有什么建议吗?

编辑: 正确的做法是:

UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
[navigationBarAppearance setBarTintColor:myBrownColor];

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:myLightBrownColor,      NSForegroundColorAttributeName,
    myShadow, NSShadowAttributeName,
    [UIFont fontWithName:@"AmericanTypewriter" size:16.0], NSFontAttributeName, nil];
[navigationBarAppearance setTitleTextAttributes:textTitleOptions];

这会将字体更改为“American TypeWriter”。

但我有一个名为“Pacifico”的自定义字体,它位于我的“支持文件”文件夹中,名称为“Pacifico.ttf”,并在我的 info.plist 中列为“应用程序提供的字体”,但字体是'没有改变,所以不知何故没有正确引用自定义字体?

【问题讨论】:

  • 如何在第一个代码 sn-p 中初始化navigationBarAppearance?这段代码在您的应用委托/视图控制器中的什么位置?
  • UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
  • 代码在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions末尾
  • 你是如何初始化myShadow
  • 同时尝试非自定义字体/确保 UIFont 对象不为零。

标签: ios uinavigationbar font-face uicolor


【解决方案1】:

导航栏标题使用

self.navigationController.navigationBar.titleTextAttributes= @{
   NSForegroundColorAttributeName: [UIColor purpleColor],
   NSFontAttributeName: [UIFont robotoMediumFontSize : 20.0],
};
self.navigationItem.title=@"MY Title";

用于设置导航栏背景颜色使用

self.navigationController.navigationBar.barTintColor=[UIColor myColor];

【讨论】:

  • 是的..你可以在Appdelegate中写如下
  • 访问appdelegate中的导航控制器写UINavigationController *navigationController =(UINavigationController *)self.window.rootViewController;
  • 然后做navigationController.navigationBar.titleTextAttributes=@{ NSForegroundColorAttributeName: [UIColor purpleColor], NSBackgroundColorAttributeName: [UIColor blueColor], NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:30.0], };
  • navigationController.navigationBar.barTintColor=[UIColor myColor]; 然后在每个 ViewController 中写入 self.title = @"Your navigation title"; 现在您将看到应用于此标题和导航栏的所有属性
【解决方案2】:

我找到了解决方案,字体需要如上所述添加到 info.plist 中,但另外在 ---> Build Phases ---> Copy Bundle Resources 中添加。 p>

【讨论】:

    猜你喜欢
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多