I have navigation bar controller in which i want to display text. But the text is not the same every time. It will be minimum of 5 and maximum of 60 characters. I want all of the characters to be visible. How can I set min and max font size for UINavigation Bar title?
Thanx
here is the answer
UILabel *bigLabel = [[UILabel alloc] init];
bigLabel.text = #"1111111122222233333344444455555556666777888899999000000";
bigLabel.backgroundColor = [UIColor clearColor];
bigLabel.textColor = [UIColor whiteColor];
bigLabel.font = [UIFont boldSystemFontOfSize:20];
bigLabel.adjustsFontSizeToFitWidth = YES;
[bigLabel sizeToFit];
self.navigationItem.titleView = bigLabel;
You will need to set the titleView on the UINavigationBar. You can set it to a UILabel with any of the normal UILabel settings.
Related
This question looks very usual.. but I had a problem yesterday, I have added a label in xib and created outlet for it, I want to have multiple lines, buttons inside that label, So I have decided to add new sub labels inside it...
UILabel *label;
label = [[UILabel alloc] initWithFrame:initial_rect];
label.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
label.text = sel_obj->start_time;
label.backgroundColor = [UIColor whiteColor];
[xib_label addSubview:label]
When I tried like that, it was not working, then I have added the same label in self.view it works fine.. So what do I need to do when I want to add a label within a label which was added using xib. Am I missing anything here.. thanks...
I came across this situation long ago.
Just FYI :
I think the problem is the initial_rect, which is in the view's frame but out of the xib_label's frame.
The frame of label is relative to xib_label, not self.view.
You can try this :
UILabel *label;
CGRect rect = (CGRect){0, 0, 10, 10};
label = [[UILabel alloc] initWithFrame:rect];
label.font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]];
label.text = sel_obj->start_time;
label.backgroundColor = [UIColor redColor];
[xib_label addSubview:label];
Change the backgroundColor to red to see the label clearly.
And if you want to show multiple lines in a label, you can :
label.numberOfLines = 0;.
you can also try after the above comments to bring subview to front [xib_lable bringSubViewToFront:label];
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
iPhone Navigation Bar Title text color
How do I set the title color of the navigation bar through code?
You need to use a UILabel as the titleView of the navigationItem.
Set label background color as clear color and text color as according to your requirement.
then set the titleview for navigation item to this label.
self.navigationItem.titleView = label;
You can use uinavigationbar's setTitleTextAttributes for setting the font, color, offset, and shadow color.
this works for ios 5.
CGRect frame = CGRectMake(0, 0, 200, 44);
TitleLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
TitleLabel.backgroundColor = [UIColor clearColor];
TitleLabel.font = [UIFont boldSystemFontOfSize:14.0];
TitleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
TitleLabel.text = #"Nav Bar Title";
TitleLabel.textAlignment = UITextAlignmentCenter;
TitleLabel.textColor = [UIColor colorWithRed:1 green:2 blue:4 alpha:0.7];
self.navigationItem.titleView = TitleLabel.text;
You can take a UILabel, set its font, color alignment and add it on Navigation bar.
Hope it works for you.
In my ViewDidLoad I try something like that but its not working:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,2,60,14)];
label.autoresizingMask = UIViewAutoresisingFlecibleWidth;
[self.navigationController.navigationBar addSubview:label];
I want to display a label in the left part of my NavigationBar.
Or maybe change the navigationBar.title position
Thanks,
You can do set navigation item's title view to UILabel with left text alignment:
UILabel* lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = UITextAlignmentLeft;
lbNavTitle.text = NSLocalizedString(#"Hello World!",#"");
self.navigationItem.titleView = lbNavTitle;
[lbNavTitle release];
Created this way item's title also tolerates buttons on navigation bar and does not overlap them.
i want change title's color of navigation controller but i dont know why doesn't work !
UILabel *label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor yellowColor];
self.navigationItem.titleView = label;
i figured out my problem :
CGRect frame = CGRectMake(0, 0, 310, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:25];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor brownColor];
self.navigationItem.titleView = label;
label.text = NSLocalizedString(#"YOUR TEXT", #"");
now how can add a logo on my navigation bar ? :D
When you assign the UILabel your not giving it a size so its ending up with a frame of 0x0 - by adding [label sizeToFit]; after you've added the text you'll get what you want.
Or you can set the size of the label when you create it with initWithFrame:(CGRect)frame
Do you want to change the color of the whole navigation bar or just the UILabel in the middle of it? Assuming you want to recolor the whole bar, you can do it in Interface Builder by setting the tint color, or you can use navigationController.navigationBar.tintColor = UIColor(...)
I have a simple navigation based application for the iphone/objective-c
within various UIViewControllers that are pushed into view, I can set the text in the title bar using something like
self.title = #"blah blah blah"
Is there a way to control the font and font-size of the title in the title bar text?
thanks!
the proper way to resize the title text of a navcontroller is to set the titleView property of the navigationItem
like this (in viewDidLoad)
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.navigationItem.title;
tlabel.textColor=[UIColor whiteColor];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;
You may want to emboss the label so it doesn't look fuzzy and flat:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:18.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.navigationItem.title;
// emboss so that the label looks OK
[label setShadowColor:[UIColor darkGrayColor]];
[label setShadowOffset:CGSizeMake(0, -0.5)];
self.navigationItem.titleView = label;
}
IF you want this to work both on iphone and ipad, and also want to get the title centered then use the following code.
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, self.navigationItem.titleView.frame.size.width, 40)];
label.text=self.navigationItem.title;
label.textColor=[UIColor whiteColor];
label.backgroundColor =[UIColor clearColor];
label.adjustsFontSizeToFitWidth=YES;
label.font = [AppHelper titleFont];
label.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView=label;
}
You can assign any UIView to a navcontroller's title area.
Create a UILabel and set its font and size anyway you want, then assign it to the UIViewController's navigationItem.titleView property. Make sure the UILabel's backgroundColor is set to clearColor.
This only works on the top-level nav-view. As the user drills down into the view controller hierarchy and the "back" button is shown the alternate titleView is ignored and the regular text label is shown.