gpt4 book ai didi

iphone - 如何在 UINavigationController 中添加多行标题栏

转载 作者:可可西里 更新时间:2023-11-01 06:19:46 24 4
gpt4 key购买 nike

我尝试在 UINavigationController 中添加两行标题栏

我想根据字符串长度自动调整字体大小。我的字符串最大大小为 60。我已尝试通过以下代码实现

UILabel *bigLabel = [[UILabel alloc] init];
bigLabel.text = @"1234567890 1234567890 1234567890 1234567890 1234567890 123456";
bigLabel.backgroundColor = [UIColor clearColor];
bigLabel.textColor = [UIColor whiteColor];
bigLabel.font = [UIFont boldSystemFontOfSize:20];
bigLabel.adjustsFontSizeToFitWidth = YES;
bigLabel.clipsToBounds = NO;
bigLabel.numberOfLines = 2;
bigLabel.textAlignment = ([self.title length] < 10 ? NSTextAlignmentCenter : NSTextAlignmentLeft);
[bigLabel sizeToFit];

self.navigationItem.titleView = bigLabel;

这对我不起作用,你能帮帮我吗?我必须为 iPhone 和 iPad 屏幕制作这个

最佳答案

只需设置 setNumberOfLines: 0UILabel。请参见下面的示例。

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[label setBackgroundColor:[UIColor clearColor]];
[label setNumberOfLines:0];
[label setTextColor:[UIColor whiteColor]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setText:@"1234567890 1234567890 1234567890 1234567890 1234567890 123456"];
self.navigationItem.titleView = label;

enter image description here

设置 Left 和 Right UIBarButtonItems - 您可以添加它们。

UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:self action:@selector(leftPress:)];
self.navigationItem.leftBarButtonItem = leftBarButton;

UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightPress:)];
self.navigationItem.rightBarButtonItem = rightBarButton;

同时减小LabelFontSize。在本例中为 12。它看起来像这样:

Enter image description here

对于扩展问题:

只需对之前的代码做一些改动-

[label setNumberOfLines:2]; //Set it 2 instead of 0

NSString *titleStr = @"3456456676456464554541223434484384233456744444444456785643367";

//Check your string length then set font according to it.
//I have set font size according to your requirement
//which is 0-60.

if([titleStr length]>40 && [titleStr length]<=52){
[label setFont:[UIFont fontWithName:@"Arial" size:13.0]];
}
else if([titleStr length]>52){
[label setFont:[UIFont fontWithName:@"Arial" size:11.5]];
}

[label setText:titleStr];

注意:您不能使用 UILabeladjustsFontSizeToFitWidth: 属性,因为它不适用于 setNumberOfLines:0 在您的情况下,您必须使用 if 条件来处理它。

这是根据 UILabelwidth 设置 fontSize 的方法。

[label setNumberOfLines:1];
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 1.0;

关于iphone - 如何在 UINavigationController 中添加多行标题栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12985401/

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