[英]Font size is not changed for title and subtitle in navigationItem.titleView
我的目標是在導航控制器頁面標題中使用標題和帶有不同字體大小的字幕(標題應該更大,字幕應該相應地更低)。
我找到了實現此目的的代碼示例。 唯一的問題是未應用字體大小-標題和副標題都具有相同的字體大小。 像字體大小的代碼不起作用。
如何解決? 謝謝
// 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;
使用setFont
方法,而不是.font
[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12.0]];
而且您在字體名稱中有一個錯誤:
it's HelveticaNeue-Light
使用SetFont代替Font ::
[titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:14.0]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.