UINavigationController default bar title sucks. Come on, we all, at least one time, need the possibility to write two lines! One for the title and another one for the subtitle.
This is my Objective-C method.
/** Create a View for displaying a title and a subtitle to replace the default UINavigationController title.
You should manually set the UINavigationController title (navigationItem.title = title;) so that others ViewControllers would see that title instead of "Back" when needed.
@param title String representing the title.
@param subtitle String representing the subtitle.
@return Returns the UIView containing title and subtitle.
*/
+ (UIView *)createNavigationTitleViewWithTitle:(NSString *)title andSubtitle:(NSString *)subtitle {
if (subtitle == nil) {
subtitle = @"";
}
const NSInteger leftOffset = 15;
// Replace titleView.
UIView *headerTitleSubtitleView = [[UILabel alloc] initWithFrame:CGRectMake(leftOffset, 0, 200, 44)];
headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
headerTitleSubtitleView.autoresizesSubviews = YES;
CGRect frame = [subtitle isEqualToString:@""] ? CGRectMake(leftOffset, 0, 160, 44) : CGRectMake(leftOffset, 2, 160, 24);
UILabel *titleView = [[UILabel alloc] initWithFrame:frame];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:19];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = title;
titleView.adjustsFontSizeToFitWidth = YES;
titleView.minimumScaleFactor = 0;
titleView.lineBreakMode = NSLineBreakByTruncatingMiddle;
[headerTitleSubtitleView addSubview:titleView];
// If subtitle is not empty...
if (![subtitle isEqualToString:@""]) {
UILabel *subtitleView = [[UILabel alloc] initWithFrame:CGRectMake(leftOffset, 24, 160, 44-24)];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.font = [UIFont boldSystemFontOfSize:13];
subtitleView.textAlignment = NSTextAlignmentCenter;
subtitleView.textColor = [UIColor whiteColor];
subtitleView.shadowColor = [UIColor darkGrayColor];
subtitleView.shadowOffset = CGSizeMake(0, -1);
subtitleView.text = subtitle;
subtitleView.adjustsFontSizeToFitWidth = YES;
subtitleView.minimumScaleFactor = 4;
subtitleView.lineBreakMode = NSLineBreakByTruncatingMiddle;
[headerTitleSubtitleView addSubview:subtitleView];
}
return headerTitleSubtitleView;
}
Download the ready-for-use source file (.m) of Title and subtitle in the UINavigation Bar (AKA title in two lines)
No comments:
Post a Comment