gpt4 book ai didi

ios - UINavigationBar 中的 AttributedString 不显示两行文本

转载 作者:可可西里 更新时间:2023-11-01 04:43:56 24 4
gpt4 key购买 nike

我正在尝试在我的 UINavigationBar 中设置两行标签。当我执行以下操作时,它只显示一行(换行符之前的部分)。

NSString *title = @"First line of title is bigger\nSecond line of title is smaller";
NSDictionary *attribute1 = @{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 14.0f]};
NSDictionary *attribute2 = @{NSForegroundColorAttributeName: [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1],
NSFontAttributeName: [UIFont boldSystemFontOfSize: 10.0f]};
const NSRange line1Range = {0, 29};
const NSRange line2Range = {31, 30};

NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:title];
[attributedText setAttributes: attribute1 range:line1Range];
[attributedText setAttributes: attribute2 range:line2Range];

UILabel *label = [[UILabel alloc] init];
label.attributedText=attributedText;
self.navigationItem.titleView = label;
[self.navigationItem.titleView sizeToFit];

为了比较,下面显示了两条线。以下仅供比较。顶级版本是我需要的。

UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 14.0f];
label.textColor = [UIColor colorWithRed:148/255.0f green:147/255.0f blue:147/255.0f alpha:1];
label.text = @"First line of title is bigger\nSecond line of title is smaller";

self.navigationItem.titleView = label;
[self.navigationItem.titleView sizeToFit];

对于让顶级版本正常工作有什么帮助吗?

最佳答案

似乎调用 label.sizeToFit() before self.navigationItem.titleView = label 正确显示 NSAttributedString 标题。

( swift ,iOS8.1,XCode 6.1.1)

关于ios - UINavigationBar 中的 AttributedString 不显示两行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25221944/

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