【发布时间】:2009-10-09 20:47:31
【问题描述】:
我有一个用于 iphone/objective-c 的简单导航应用程序
在推送到视图中的各种 UIViewControllers 中,我可以使用类似的东西在标题栏中设置文本
self.title = @"blah blah blah"
有没有办法控制标题栏文字中标题的font和font-size?
谢谢!
【问题讨论】:
标签: iphone objective-c
我有一个用于 iphone/objective-c 的简单导航应用程序
在推送到视图中的各种 UIViewControllers 中,我可以使用类似的东西在标题栏中设置文本
self.title = @"blah blah blah"
有没有办法控制标题栏文字中标题的font和font-size?
谢谢!
【问题讨论】:
标签: iphone objective-c
调整navcontroller标题文本大小的正确方法是设置navigatorItem的titleView属性
像这样(在 viewDidLoad 中)
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.navigationItem.title;
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;
【讨论】:
您可能需要对标签进行压印,使其看起来不模糊和扁平:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:18.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.navigationItem.title;
// emboss so that the label looks OK
[label setShadowColor:[UIColor darkGrayColor]];
[label setShadowOffset:CGSizeMake(0, -0.5)];
self.navigationItem.titleView = label;
}
【讨论】:
label.font = [UIFont boldSystemFontOfSize:18.0]; 这似乎非常接近导航栏标题的原始字体。
如果您希望它在 iphone 和 ipad 上都可以使用,并且还希望标题居中,请使用以下代码。
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
label.text=self.navigationItem.title;
label.textColor=[UIColor whiteColor];
label.backgroundColor =[UIColor clearColor];
label.adjustsFontSizeToFitWidth=YES;
label.font = [AppHelper titleFont];
label.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView=label;
}
【讨论】:
您可以将任何 UIView 分配给导航控制器的标题区域。
创建一个UILabel 并根据需要设置其字体和大小,然后将其分配给UIViewController 的navigationItem.titleView 属性。确保 UILabel 的 backgroundColor 设置为 clearColor。
这仅适用于顶级导航视图。当用户深入查看视图控制器层次结构并显示“后退”按钮时,替代的 titleView 将被忽略并显示常规文本标签。
【讨论】: