Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Change font size in navigation bar

AngerikaAngerika Posts: 19Registered Users
edited September 2011 in iPhone SDK Development
How can I change de size of the title in my navigation bar???
Post edited by Angerika on

Replies

  • javid.alimohideenjavid.alimohideen Posts: 298Registered Users
    edited May 2008
    I haven't tried this yet. But my guess, would be to create a UILabel and set it's font and size and assign it as the titleView of the navigation Item;
    UILabel *bigLabel = [[UILabel alloc] init];
    bibLabel.text = @"I am BIG";
    bigLabel.font = [UIFont fontWithName:@"Arial" size: 22.0];
    self.navigationItem.titleView = bigLabel;
    [bigLabel release];
    
  • AngerikaAngerika Posts: 19Registered Users
    edited May 2008
    I put this code in my loadView method, but it doesn't shows anything :|

    UILabel *bigLabel = [[UILabel alloc] init];
    bigLabel.text = @"I am BIG";
    bigLabel.font = [UIFont fontWithName:@"Arial" size: 22.0];
    self.navigationItem.customTitleView = bigLabel;
    [bigLabel release];
  • javid.alimohideenjavid.alimohideen Posts: 298Registered Users
    edited May 2008
    Hmmm. I don't have access to my mac right now to test the code.
    Maybe, the font name is not a valid one,
    test it with code below
    UILabel *bigLabel = [[UILabel alloc] init];
    bibLabel.text = @"I am BIG";
    bigLabel.font = [UIFont systemFontOfSize:22.0];
    self.navigationItem.titleView = bigLabel;
    [bigLabel release];
    
    Also, I noticed you are using customTitleView property in setting the navigation item. I don't think that is still available in SDK5, so use the titleView property.

    Make sure, you don't have any warnings. Objective C is dynamically typed, so it's better to
    TREAT WARNINGS AS ERRORS.
  • AngerikaAngerika Posts: 19Registered Users
    edited May 2008
    This is my code for showing a label in my navigationBar title:

    UILabel *myTitle = [[UILabel alloc] init];
    myTitle.text = @"This is my title";
    self.navigationItem.titleView = myTitle;
    [myTitle release];

    but it doesn't show anything, what's wrong?
  • halushahalusha Posts: 71Registered Users
    edited May 2008
    Try setting myTitle.frame to something reasonable.
  • javid.alimohideenjavid.alimohideen Posts: 298Registered Users
    edited May 2008
    If IB was any better, all this could be done with so much ease :(
  • boneheadbonehead Posts: 225Registered Users
    edited May 2008
    Angerika wrote:
    This is my code for showing a label in my navigationBar title:

    UILabel *myTitle = [[UILabel alloc] init];
    myTitle.text = @"This is my title";
    self.navigationItem.titleView = myTitle;
    [myTitle release];

    but it doesn't show anything, what's wrong?

    The docs for the UINavigationItem titleView property say that if the nav item's left button is non-nil, the custom title view is not displayed:
    This property is ignored if leftBarButtonItem is not nil.

    So if you have a left button (such as the default "back" button), it's probably ignored.
  • Pedro ValentiniPedro Valentini Posts: 34Registered Users
    edited November 2008
    I`m using this code to customize title view, its work for me, if you have problem I sugest that try to localize title view in stack of items.. in my case it`s working because topItem is the title view...

    Insert in loadview..
    	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    	[label setFont:[UIFont boldSystemFontOfSize:16.0]];
    	[label setBackgroundColor:[UIColor clearColor]];
    	[label setTextColor:[UIColor whiteColor]];
    	[label setText:self.title];
    	[self.navigationController.navigationBar.topItem setTitleView:label];
    	[label release];
    
    <a href="http://www.tecnalta.net" target="_blank">tecnalta.net</a>
  • ghanalupoghanalupo Posts: 133Registered Users
    edited September 2009
    I`m using this code to customize title view, its work for me, if you have problem I sugest that try to localize title view in stack of items.. in my case it`s working because topItem is the title view...

    Insert in loadview..
    	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    	[label setFont:[UIFont boldSystemFontOfSize:16.0]];
    	[label setBackgroundColor:[UIColor clearColor]];
    	[label setTextColor:[UIColor whiteColor]];
    	[label setText:self.title];
    	[self.navigationController.navigationBar.topItem setTitleView:label];
    	[label release];
    

    Works exactly as required! Thanks for this ;)
  • myemailisjustinmyemailisjustin Posts: 52Registered Users
    edited October 2009
    ghanalupo wrote: »
    Works exactly as required! Thanks for this ;)

    It was crashing for me, unless I changed the following line:
    	[self.navigationController.navigationBar.topItem setTitleView:label];
    
    to:
    	[self.navigationController.navigationBar.topItem setTitleView:label.text];
    

    Then it works! Thanks!
    Malus Pumila, LLC<br />
    <a href="http://www.maluspumila.net" target="_blank">http://www.maluspumila.net</a><br />
    <br />
    God's Bible - <a href="http://www.maluspumila.net/Gods_Bible_-_The_iPhone_Bible_Application/Welcome.html" target="_blank">http://ww
  • mattskimattski Posts: 1New Users
    edited June 2010
    I haven't tried this yet. But my guess, would be to create a UILabel and set it's font and size and assign it as the titleView of the navigation Item;

    You just missed one thing - to size the view. This can be done either with the initWithFrame intialiser, or by calling sizeToFit like this:
    UILabel *bigLabel = [[UILabel alloc] init];
    bigLabel.text = @"I am BIG";
    bigLabel.font = [UIFont fontWithName:@"Arial" size: 22.0];
    [bigLabel sizeToFit];
    self.navigationItem.titleView = bigLabel;
    [bigLabel release];
    
  • CeiJayCeiJay Posts: 21Registered Users
    edited July 2010
    This is your best option:
    // List of Fonts Available on the iPhone - http://ajnaware.wordpress.com/2008/10/24/list-of-fonts-available-on-the-iphone/
    	UILabel *label = [[UILabel alloc] init];
    	label.font = [UIFont fontWithName:@"Helvetica-Bold" size: 15.0];
    	// Optional - label.text = @"NavLabel";
    	[label setBackgroundColor:[UIColor clearColor]];
    	[label setTextColor:[UIColor whiteColor]];
    	[label setText:self.title];
    	[label sizeToFit];
    	[self.navigationController.navigationBar.topItem setTitleView:label];
    	[label release];
    
    <a href="http://www.twitter.com/tailendchuck" target="_blank">Follow Me on Twitter</a>
  • Ice_2kIce_2k Posts: 154Registered Users @ @
    edited July 2010
    Here's how I'm doing it:
    - (void)SetNavigationTitle:(NSString*)title AndSubtitle:(NSString*)subtitle
    {
      if (subtitle == nil)
      {
        self.navigationItem.titleView = nil;
        self.navigationItem.title = title;
        return;
      }
      
      // set the default title anyway, so the next view controllers will have the correct text on their "back" button
      self.navigationItem.title = title;
      
      #define LEFT_OFFSET 15
      
      // Replace titleView
      CGRect headerTitleSubtitleFrame = CGRectMake(LEFT_OFFSET, 0, 200, 44);    
      UIView* _headerTitleSubtitleView = [[UILabel alloc] initWithFrame:headerTitleSubtitleFrame];
      _headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
      _headerTitleSubtitleView.autoresizesSubviews = YES;
      
      CGRect titleFrame = CGRectMake(LEFT_OFFSET, 2, 160, 24);  
      UILabel *titleView = [[UILabel alloc] initWithFrame:titleFrame];
      titleView.backgroundColor = [UIColor clearColor];
      titleView.font = [UIFont boldSystemFontOfSize:20];
      titleView.textAlignment = UITextAlignmentCenter;
      titleView.textColor = [UIColor whiteColor];
      titleView.shadowColor = [UIColor darkGrayColor];
      titleView.shadowOffset = CGSizeMake(0, -1);
      titleView.text = title;
      titleView.adjustsFontSizeToFitWidth = YES;
      [_headerTitleSubtitleView addSubview:titleView];
      [titleView release];
      
      CGRect subtitleFrame = CGRectMake(LEFT_OFFSET, 24, 160, 44-24);   
      UILabel *subtitleView = [[UILabel alloc] initWithFrame:subtitleFrame];
      subtitleView.backgroundColor = [UIColor clearColor];
      subtitleView.font = [UIFont boldSystemFontOfSize:13];
      subtitleView.textAlignment = UITextAlignmentCenter;
      subtitleView.textColor = [UIColor whiteColor];
      subtitleView.shadowColor = [UIColor darkGrayColor];
      subtitleView.shadowOffset = CGSizeMake(0, -1);
      subtitleView.text = subtitle;
      subtitleView.adjustsFontSizeToFitWidth = YES;
      [_headerTitleSubtitleView addSubview:subtitleView];
      [subtitleView release];
      
      self.navigationItem.titleView = _headerTitleSubtitleView;
      [_headerTitleSubtitleView release];
    }
    
    <a href="http://itunes.apple.com/ro/app/autovit/id403220907?mt=8" target="_blank">My 1st app</a><br />
    <a href="http://itunes.apple.com/us/app/fuel-monitor/id410088611?mt=8" target="_blank">My 2nd app</a>
  • cpcdevcpcdev Posts: 99Registered Users
    edited September 2010
    mattski wrote: »
    You just missed one thing - to size the view. This can be done either with the initWithFrame intialiser, or by calling sizeToFit like this:
    UILabel *bigLabel = [[UILabel alloc] init];
    bigLabel.text = @"I am BIG";
    bigLabel.font = [UIFont fontWithName:@"Arial" size: 22.0];
    [bigLabel sizeToFit];
    self.navigationItem.titleView = bigLabel;
    [bigLabel release];
    

    How do I set the label in the above code to show no background? With the above code the label has a white background.

    Thanks
  • cpcdevcpcdev Posts: 99Registered Users
    edited September 2010
    cpcdev wrote: »
    How do I set the label in the above code to show no background? With the above code the label has a white background.

    Thanks

    ok so I used bigLabel.backgroundColor = [UIColor blueColor]; and that changes the color but how do I set it to show without a background?
  • musicwind95musicwind95 Posts: 365Registered Users
    edited September 2010
    bigLabel.backgroundColor = [[UIColor alloc] initWithWhite:1.0 alpha:0.0];
    If I have helped you, <a href="http://bit.ly/mw95donate" target="_blank">please consider donating</a>. I use PayPal. It would mean a lot to me!<br />
    <br />
    <a href="http://cupsofcocoa.wordpress.com/" target="_blank">20100829-
  • cpcdevcpcdev Posts: 99Registered Users
    edited September 2010
    cpcdev wrote: »
    ok so I used bigLabel.backgroundColor = [UIColor blueColor]; and that changes the color but how do I set it to show without a background?

    fixed it! used "clearColor"
  • pierreabreuppierreabreup Posts: 1New Users
    edited April 2011
    I`m using this code to customize title view, its work for me, if you have problem I sugest that try to localize title view in stack of items.. in my case it`s working because topItem is the title view...

    Insert in loadview..
    	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    	[label setFont:[UIFont boldSystemFontOfSize:16.0]];
    	[label setBackgroundColor:[UIColor clearColor]];
    	[label setTextColor:[UIColor whiteColor]];
    	[label setText:self.title];
    	[self.navigationController.navigationBar.topItem setTitleView:label];
    	[label release];
    

    VLW PELA AJUDA AQUELE ABRA
  • SO9769SO9769 Posts: 3New Users Noob
    edited September 2011
    I haven't tried this yet. But my guess, would be to create a UILabel and set it's font and size and assign it as the titleView of the navigation Item;
    UILabel *bigLabel = [[UILabel alloc] init];
    bibLabel.text = @"I am BIG";
    bigLabel.font = [UIFont fontWithName:@"Arial" size: 22.0];
    self.navigationItem.titleView = bigLabel;
    [bigLabel release];
    

    Hello,

    How would I change the font size without changing the font?

    Regards
  • baja_yubaja_yu Posts: 6,188Moderators, Registered Users Admin
    edited September 2011
    SO9769 wrote: »
    Hello,

    How would I change the font size without changing the font?

    Regards
    [UIFont systemFontOfSize:22.0];
    
Sign In or Register to comment.