iPhone 导航栏标题文字颜色 [英] iPhone Navigation Bar Title text color
问题描述
iOS 导航栏标题颜色似乎默认为白色.有没有办法把它改成不同的颜色?
It seems the iOS Navigation Bar title color is white by default. Is there a way to change it to a different color?
我知道使用图像的 navigationItem.titleView
方法.由于我的设计能力有限,未能达到标准光泽度,我更喜欢更改文字颜色.
I am aware of the navigationItem.titleView
approach using an image. Since my design skills are limited and I failed to get the standard glossy, I prefer changing the text color.
任何见解将不胜感激.
推荐答案
现代方法
现代方式,对于整个导航控制器......在导航控制器的根视图加载后执行一次.
Modern approach
The modern way, for the entire navigation controller… do this once, when your navigation controller's root view is loaded.
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor yellowColor]}];
但是,这在后续视图中似乎没有影响.
However, this doesn't seem have an effect in subsequent views.
旧方法,每个视图控制器(这些常量适用于 iOS 6,但如果想在 iOS 7 外观上为每个视图控制器执行此操作,您将需要相同的方法,但使用不同的常量):
The old way, per view controller (these constants are for iOS 6, but if want to do it per view controller on iOS 7 appearance you'll want the same approach but with different constants):
您需要使用 UILabel
作为 navigationItem
的 titleView
.
You need to use a UILabel
as the titleView
of the navigationItem
.
标签应该:
- 具有清晰的背景颜色(
label.backgroundColor = [UIColor clearColor]
). - 使用粗体 20pt 系统字体 (
label.font = [UIFont boldSystemFontOfSize: 20.0f]
). - 具有 50% alpha 的黑色阴影(
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]
). - 您还需要将文本对齐方式设置为居中(
label.textAlignment = NSTextAlignmentCenter
(UITextAlignmentCenter
用于旧版 SDK).
- Have a clear background color (
label.backgroundColor = [UIColor clearColor]
). - Use bold 20pt system font (
label.font = [UIFont boldSystemFontOfSize: 20.0f]
). - Have a shadow of black with 50% alpha (
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]
). - You'll want to set the text alignment to centered as well (
label.textAlignment = NSTextAlignmentCenter
(UITextAlignmentCenter
for older SDKs).
将标签文本颜色设置为您喜欢的任何自定义颜色.您确实需要一种不会导致文本混合到难以阅读的阴影中的颜色.
Set the label text color to be whatever custom color you'd like. You do want a color that doesn't cause the text to blend into shadow, which would be difficult to read.
我通过反复试验解决了这个问题,但我想出的值最终太简单了,它们不是 Apple 选择的.:)
I worked this out through trial and error, but the values I came up with are ultimately too simple for them not to be what Apple picked. :)
如果您想验证这一点,请将此代码放入 Apple 的 NavBar 示例.这将用黄色标签替换文本.这应该与 Apple 代码生成的原始代码没有区别,除了颜色.
If you want to verify this, drop this code into initWithNibName:bundle:
in PageThreeViewController.m
of Apple's NavBar sample. This will replace the text with a yellow label. This should be indistinguishable from the original produced by Apple's code, except for the color.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// this will appear as the title in the navigation bar
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = NSTextAlignmentCenter;
// ^-Use UITextAlignmentCenter for older SDKs.
label.textColor = [UIColor yellowColor]; // change this color
self.navigationItem.titleView = label;
label.text = NSLocalizedString(@"PageThreeTitle", @"");
[label sizeToFit];
}
return self;
}
另外,请阅读下面 Erik B 的回答.我的代码显示了效果,但他的代码提供了一种更简单的方法来将其放置在现有视图控制器上.
Also, read Erik B's answer below. My code shows the effect, but his code offers a simpler way to drop this into place on an existing view controller.
这篇关于iPhone 导航栏标题文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!