Set the color of a view's title?

Discussion in 'iOS Programming' started by Danneman101, May 20, 2009.

  1. Danneman101 macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    Is it possible to set the color of the top-title of a view, ie. the one appearing on top of everything ("Testing" on this image: http://pessoal.org/blog/wp-content/uploads/2009/02/standard-200x300.png)?

    Ive tried:

    Code:
    self.title = [UIColor lightGrayColor];
    // or
    self.title.textColor = [UIColor lightGrayColor];
    
    ..but no success.

    I know Im pointing to the correct title with "self.title" since:

    Code:
    self.title = @"Dude";
    
    ..does in fact change the title to "Dude".
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What you're really changing here is the title property of a UINavigationItem. And since title is just an NSString, no, you can't change its color. You may be able to achieve what you want by placing a UILabel into the titleView and going from there.
     
  3. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #3
    yes, that works. like dejo said, you have the an UINavigationItem here. If you read through the reference of UINavigationItem
    http://developer.apple.com/iphone/l...ionItem_Class/Reference/UINavigationItem.html
    you will see that instead of the title you can set a titleView property, which can be any kind of UIView. With this, you can for example display an image as the title (UIImageView) or, that's what you want, an UILabel.
     
  4. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #4
    Ah, I see :)

    I must be doing something wrong, though, since now Im not getting any title at all - not "Hello" and not the previous standard one I had without this code.

    Code:
    UILabel *myLabel = [[UILabel alloc] init];
    myLabel.text = @"Hello";
    myLabel.textColor = [UIColor lightGrayColor];
    self.navigationItem.titleView = myLabel;
    
     
  5. fishkorp macrumors 68020

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #5
    I just add my custom view (label, image, etc) as a subview to the navigation item's view.
     
  6. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #6
    possible, but not the official way :D
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    You haven't set the frame for myLabel.

    Also, be careful. titleView is ignored if leftBarButtonItem is not nil.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    @Danneman101, give your label a frame and use initWithFrame. Yours has no size. You might also set the autoResizemask.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Just realize that you are going to have to do a bunch of formatting (font, backgroundColor, etc.) to your label to get it even close in appearance to the default title look.
     
  10. Danneman101 thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #10
    Thanks guys for your help :)

    Yes, a lot of formatting seems to be needed. The biggest problem seem to get the label from not moving in realtime, and hit the exact middle spot in the GCRectMake.

    Code:
    	// --------------------
    	// SET:		Title-Label
    	// --------------------
    	UILabel *myLabel = [[UILabel alloc] init];
    	myLabel.frame = CGRectMake(150, 0, 100, 44); //x,y,width,height	
    	myLabel.textColor = [UIColor lightGrayColor];
    	myLabel.backgroundColor = [UIColor clearColor];
    	myLabel.text = self.title;
    	self.navigationItem.titleView = myLabel;	
    
     

Share This Page