Reputation: 12051
I have a custom UINavigationBar
with a 2 line label in its titleView
:
UILabel *navBarLabel = [[UILabel alloc] initWithFrame:CGRectZero];
UINavigationItem *item = [[UINavigationItem alloc] init];
navBarLabel.backgroundColor = [UIColor clearColor];
navBarLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
navBarLabel.numberOfLines = 2;
navBarLabel.textAlignment = UITextAlignmentCenter;
navBarLabel.textColor = [UIColor colorWithRed:124.0/255.f green:125.0/255.f blue:128.0/255.f alpha:1.0];
navBarLabel.text = @"This\nis an example";
[navBarLabel sizeToFit];
item.titleView = navBarLabel;
But instead of making my UILabel
with 2 lines of texts, I would like to add 2 UILabels
, one on top of another to achieve the customisation of the font in each of the 'lines' separately. How is it implemented? Any ideas?
Upvotes: 1
Views: 3976
Reputation: 303
iOS allow you add two item in the bar. Try the following code.
UIButton *tempButton = nil;
UIImage *tempButtonImage = nil;
UIImage *tempButtonPressedImage = nil;
tempButtonImage = [UIImage imageNamed:@"unselected1.png"];
tempButtonPressedImage = [UIImage imageNamed:@"selected1.png"];
tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tempButton setImage : tempButtonImage forState : UIControlStateNormal];
[tempButton setImage : tempButtonPressedImage forState : UIControlStateHighlighted];
[tempButton addTarget : self action : @selector(tempButtonClick:) forControlEvents : UIControlEventTouchUpInside];
tempButton.frame = CGRectMake(0, 0, tempButtonImage.size.width, tempButtonImage.size.height);
UIView *tempButtonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, tempButtonImage.size.width, tempButtonImage.size.height}];
tempButtonContainer.backgroundColor = [UIColor clearColor];
[tempButtonContainer addSubview:tempButton];
UIBarButtonItem *tempToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:tempButtonContainer];
// custom logout button
UIButton *temp2Button = nil;
UIImage *temp2ButtonImage = nil;
UIImage *temp2ButtonPressedImage = nil;
temp2ButtonImage = [UIImage imageNamed:@"unselectedtemp2Button.png"];
temp2ButtonPressedImage = [UIImage imageNamed:@"selectedtemp2Button.png"];
temp2Button = [UIButton buttonWithType:UIButtonTypeCustom];
[temp2Button setImage : temp2ButtonImage forState : UIControlStateNormal];
[temp2Button setImage : temp2ButtonPressedImage forState : UIControlStateHighlighted];
[temp2Button addTarget : self action : @selector(temp2ButtonClick:) forControlEvents : UIControlEventTouchUpInside];
temp2Button.frame = CGRectMake(0, 0, temp2ButtonImage.size.width, temp2ButtonImage.size.height);
UIView *temp2ButtonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, temp2ButtonImage.size.width, temp2ButtonImage.size.height}];
temp2ButtonContainer.backgroundColor = [UIColor clearColor];
[temp2ButtonContainer addSubview:temp2Button];
UIBarButtonItem *temp2ToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:temp2ButtonContainer];
// setting right items
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:tempToolbarButton, temp2ToolbarButton, nil];
UIButton *button = nil;
UIImage *buttonImage = nil;
UIImage *buttonPressedImage = nil;
buttonImage = [UIImage imageNamed:@"asd.png"];
buttonPressedImage = [UIImage imageNamed:@"selected-asd.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage : buttonImage forState : UIControlStateNormal];
[button setImage : buttonPressedImage forState : UIControlStateHighlighted];
[button addTarget : self action : @selector(buttonClick:) forControlEvents : UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
UIView *buttonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, buttonImage.size.width, buttonImage.size.height}];
buttonContainer.backgroundColor = [UIColor clearColor];
[buttonContainer addSubview:button];
UIBarButtonItem *toolbarButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];
self.navigationItem.leftBarButtonItem = toolbarButton;
Upvotes: -2
Reputation: 31304
Just create a parent container UIView
to hold your two labels, and use this parent container as your title view. In pseudo-code:
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
[titleView addSubview:someLabel];
[titleView addSubview:anotherLabel];
item.titleView = titleView;
...all you need to make sure is that you've set your label frames correctly so that one label is positioned above the other. How you do this will depend on whether you're using auto-layout or not, but it's pretty simple and straightforward.
Upvotes: 7