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.
Related
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.
how to find length of the navigation bar....
i want to display a label centre of the navigation bar programmatically..
so that i can write formula ,so it should work if i rotate my device ...
Use the following code snippet in the viewDidLoad method of UIViewController.
UILabel *label = [[UILabel alloc] init];
label.text = #"Center Label";
label.frame = CGRectMake(0, 0, 100, 30);
label.textAlignment = NSTextAlignmentCenter;
UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithCustomView:label];
self.navigationItem.titleView = customButton.customView;
Code is self explanatory. Customize the label and other necessary stuff as required.
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 created email functionality view in which everything is working fine, but the color of a navigation bar item is not changing like other views. I have used below code for navigation color but 1 navigation bar button item is left with default color. How do I change it?
controller.navigationBar.barStyle = UIBarStyleBlack;
How about use this?
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
of course on your viewDidLoad
Do you create the bar button item yourself? If so, you can change its background color by making sure it's a "custom view" type button item:
// Assuming you have some predetermined width w and height h
UIView *backgroundView = [[[UIView alloc]
initWithFrame:CGRectMake(0.0, 0.0, w, h)] autorelease];
backgroundView.backgroundColor = [UIColor blackColor];
UIBarButtonItem *myItem = [[[UIBarButtonItem alloc]
initWithCustomView:backgroundView] autorelease];
Then you can change the background color at a future date by setting the backgroundColor property on the bar button item's customView:
myItem.customView.backgroundColor = [UIColor yellowColor];
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.