PDA

View Full Version : Set the color of a view's title?




Danneman101
May 20, 2009, 09:37 AM
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:


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:


self.title = @"Dude";


..does in fact change the title to "Dude".



dejo
May 20, 2009, 09:54 AM
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.

BlackWolf
May 20, 2009, 10:10 AM
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.

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/library/documentation/UIKit/Reference/UINavigationItem_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.

Danneman101
May 20, 2009, 12:07 PM
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.


UILabel *myLabel = [[UILabel alloc] init];
myLabel.text = @"Hello";
myLabel.textColor = [UIColor lightGrayColor];
self.navigationItem.titleView = myLabel;

fishkorp
May 20, 2009, 12:27 PM
I just add my custom view (label, image, etc) as a subview to the navigation item's view.

BlackWolf
May 20, 2009, 12:50 PM
I just add my custom view (label, image, etc) as a subview to the navigation item's view.

possible, but not the official way :D

dejo
May 20, 2009, 12:55 PM
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.


UILabel *myLabel = [[UILabel alloc] init];
myLabel.text = @"Hello";
myLabel.textColor = [UIColor lightGrayColor];
self.navigationItem.titleView = myLabel;

You haven't set the frame for myLabel.

Also, be careful. titleView is ignored if leftBarButtonItem is not nil.

PhoneyDeveloper
May 20, 2009, 01:10 PM
@Danneman101, give your label a frame and use initWithFrame. Yours has no size. You might also set the autoResizemask.

dejo
May 20, 2009, 01:22 PM
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.

Danneman101
May 20, 2009, 02:31 PM
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.


// --------------------
// 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;