简体   繁体   English

导航项.titleView中标题和字幕的字体大小未更改

[英]Font size is not changed for title and subtitle in navigationItem.titleView

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;

在此处输入图片说明

It's with the setFont method, not .font 使用setFont方法,而不是.font

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

And also you have an error in the font name: 而且您在字体名称中有一个错误:

it's HelveticaNeue-Light

使用SetFont代替Font ::

[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:14.0]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM