繁体   English   中英

如何设置导航栏字体和颜色

[英]How to set navigationbar font and colors

我正在尝试设置导航栏的颜色。 我已经在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”,在我的“ Supporting Files”文件夹中,名为“ Pacifico.ttf”,并在我的info.plist中列为“应用程序提供的字体”,但是字体没有改变,以某种方式无法正确引用自定义字体?

对于导航栏标题使用

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

用于设置导航栏背景颜色

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

我找到了解决方案,如上所述,字体需要添加到info.plist中,但是另外在--->构建阶段--->复制捆绑资源中添加了字体。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM