【发布时间】: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