设置下一个视图的导航栏标题字体?帮助“setTitleView”到一些标签?

时间:2011-08-19 15:38:24

标签: iphone ios uinavigationcontroller uinavigationbar uinavigationitem

我正在尝试设置导航栏的标题字体,所以我可以更改字体大小,因为我需要更长的标题...我这样做:

    - (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;
}

标题应该是空的......有什么建议吗?

提前多多感谢!

2 个答案:

答案 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)替换为您的值。