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?
Just create a parent container
UIViewto hold your two labels, and use this parent container as your title view. In pseudo-code:…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.