0

My goal is to use title and subtitle with different font sizes in navigation controller page title (title should be bigger, subtitle should be lower accordingly).

I found example of code to implement this. The only one problem is that font size is not applied - both title and subtitle have the same font size. Like the code for font size doesn't work.

How to fix that? Thank you

// prepare title label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:@"HelveticaNeueLight" size:19.0];
titleLabel.text = locationInfo;
[titleLabel sizeToFit];

// prepare subtitle label
UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 18, 0, 0)];
subtitleLabel.backgroundColor = [UIColor clearColor];
subtitleLabel.textColor = [UIColor whiteColor];
subtitleLabel.font = [UIFont fontWithName:@"HelveticaNeueLight" size:12.0];
subtitleLabel.text = dateInfo;
[subtitleLabel sizeToFit];

UIView *twoLineTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAX(subtitleLabel.frame.size.width, titleLabel.frame.size.width), 30)];
[twoLineTitleView addSubview:titleLabel];
[twoLineTitleView addSubview:subtitleLabel];

float widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width;

if (widthDiff > 0) {
    CGRect frame = titleLabel.frame;
    frame.origin.x = widthDiff / 2;
    titleLabel.frame = CGRectIntegral(frame);
} else{
    CGRect frame = subtitleLabel.frame;
    frame.origin.x = fabs(widthDiff) / 2;
    subtitleLabel.frame = CGRectIntegral(frame);
}

self.navigationItem.titleView = twoLineTitleView;

enter image description here

Bogdan Laukhin
  • 1,454
  • 2
  • 17
  • 26
  • Possible duplicate of [iOS change navigation bar title font and color](http://stackoverflow.com/questions/19791762/ios-change-navigation-bar-title-font-and-color) – Teja Nandamuri Apr 28 '16 at 13:26

2 Answers2

1

It's with the setFont method, not .font

[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12.0]];

And also you have an error in the font name:

it's HelveticaNeue-Light
AnthonyR
  • 3,485
  • 1
  • 18
  • 41
0

Use SetFont instead of Font::

[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:14.0]];
Mr. Bond
  • 427
  • 1
  • 4
  • 18