Martin Herman
Martin Herman

Reputation: 888

Setting navigationBar title font of the next view? Help with "setTitleView" to some label?

I'm trying to set the title font of my navigationBar, so I can change the font size, because I want a longer title... I do it like:

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

The place where the title should be is empty... Any suggestions?

Thanks a lot in advance!

Upvotes: 1

Views: 1709

Answers (2)

EmptyStack
EmptyStack

Reputation: 51374

In your code, at the time you call sizeToFit label has no text. So the frame won't get changed.

You should be calling sizeToFit method after you assign the text to the label.

Upvotes: 2

akashivskyy
akashivskyy

Reputation: 45210

You forgot to set frame of your label - replace

UILabel *navTitle = [[[UILabel alloc] init] autorelease];

with

UILabel *navTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 32)] autorelease];

You can replace CGRectMake(0, 0, 260, 32) with your values.

Upvotes: 1

Related Questions