gpt4 book ai didi

ios - navigationItem.titleView 中标题和副标题的字体大小没有改变

转载 作者:行者123 更新时间:2023-11-29 01:01:41 27 4
gpt4 key购买 nike

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

我找到了实现此功能的代码示例。唯一的问题是未应用字体大小——标题和副标题的字体大小相同。就像字体大小的代码不起作用一样。

如何解决这个问题?谢谢

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

最佳答案

是用setFont方法,不是.font

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

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

it's HelveticaNeue-Light

关于ios - navigationItem.titleView 中标题和副标题的字体大小没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36915874/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com