【问题标题】:Font size is not changed for title and subtitle in navigationItem.titleViewnavigationItem.titleView 中标题和副标题的字体大小未更改
【发布时间】:2016-08-23 06:56:05
【问题描述】:

我的目标是在导航控制器页面标题中使用不同字体大小的标题和副标题(标题应该更大,副标题应该相应地更低)。

我找到了实现这一点的代码示例。唯一的问题是没有应用字体大小——标题和副标题的字体大小相同。就像字体大小的代码不起作用。

如何解决这个问题?谢谢

// 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;

【问题讨论】:

标签: ios objective-c fonts uinavigationcontroller uinavigationitem


【解决方案1】:

使用setFont 方法,而不是.font

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

而且你的字体名称也有错误:

it's HelveticaNeue-Light

【讨论】:

  • 是的,原因是字体名称错误)非常感谢
【解决方案2】:

使用 SetFont 而不是 Font::

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    相关资源
    最近更新 更多