简体   繁体   中英

Change the Font of UINavigationBar title

i am having this code to show the button as the title of the navbar,and then in button code i write the code to change the font of the unbutton text label.but i didn't get the correct font version with the below code.

UIButton *titleButton = [[UIButton alloc]init];
titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
[titleButton setTitle:[NSString stringWithFormat:@"%@ %@",delegatee.selectedBook,delegatee.selectedChapter]forState:UIControlStateNormal];

titleButton.titleLabel.font =[UIFont fontWithName:@"Georgia" size:10.0f];
[titleButton addTarget:self action:@selector(ClickbtnChaperselection:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;

iput georgia but i didn't get the correct font Thanks.

Try this :

UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];    
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
[titleButton setTitle:[NSString stringWithFormat:@"%@ %@",delegatee.selectedBook,delegatee.selectedChapter]forState:UIControlStateNormal];

titleButton.titleLabel.font =[UIFont fontWithName:@"Georgia" size:10.0f];
[titleButton addTarget:self action:@selector(ClickbtnChaperselection:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;

Why don't you just put a label instead of a button as the title ?

Here's code for a title label:

UILabel *titleView = (UILabel *)self.navigationItem.titleView;
if (!titleView)
  {
    titleView = [[UILabel alloc] initWithFrame:CGRectZero];
    [titleView setBackgroundColor:[UIColor clearColor]];
    [titleView setFont:[UIFont fontWithName:@"Georgia" size:10.0f]];

    [self.navigationItem setTitleView:titleView];
    [titleView release];
  }
titleView.text = title;

If you need it to be UIButton, it shouldn't be too hard to adjust the code to your needs.

Good luck !

try this

for button :- use in Navigation Title view , this is work IOS 4 and IOS 5 ,I use .....

  UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];    
[titleButton setFrame:CGRectMake(0, 0, 170, 35)];
[titleButton setTitle:@"dee"forState:UIControlStateNormal];

 UIFont *font=[UIFont fontWithName:@"A_Nefel_Sereke" size:21];
titleButton.titleLabel.font =font;

titleButton.titleLabel.textColor = [UIColor redColor];
[titleButton addTarget:self action:@selector(ClickbtnChaperselection:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleButton;

I think you have problem in your font name ... Please check

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM