我正在尝试设置导航栏的标题字体,所以我可以更改字体大小,因为我需要更长的标题...我这样做:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
UILabel *navTitle = [[[UILabel alloc] init] autorelease];
[navTitle sizeToFit];
[navTitle setFont:[UIFont systemFontOfSize:30]];
[navTitle adjustsFontSizeToFitWidth];
[navTitle setBackgroundColor:[UIColor clearColor]];
[navTitle setTextColor:[UIColor whiteColor]];
[navTitle setTextAlignment:UITextAlignmentCenter];
[navTitle setText:@"SuperLongTitleOfMyNaviBar"];
[self.navigationItem setTitleView:navTitle];
}
return self;
}
标题应该是空的......有什么建议吗?
提前多多感谢!
答案 0 :(得分:2)
在您的代码中,当您致电sizeToFit
时,标签没有text
。因此frame
不会被更改。
将sizeToFit
分配给标签后,您应该调用text
方法。
答案 1 :(得分:1)
您忘了设置标签的框架 - 替换
UILabel *navTitle = [[[UILabel alloc] init] autorelease];
与
UILabel *navTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 32)] autorelease];
您可以将CGRectMake(0, 0, 260, 32)
替换为您的值。