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

我试着在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屏幕制作这个

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

 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; 

在此处输入图像描述

设置左和右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 。 它看起来像这样:

在此输入图像描述

对于扩展的问题:

只需在以前的代码中进行一些更改 –

 [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]; 

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

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

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

感谢@TheTiger提供这样的解决方案。
我根据我的要求编辑了他的答案。 所以这是我的答案

 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)]; [label setBackgroundColor:[UIColor clearColor]]; [label setTextColor:[UIColor whiteColor]]; [label setTextAlignment:NSTextAlignmentCenter]; NSString *titleStr1 = @"1234567890 1234567890 1234567890 1234567890 123 4568"; NSString *titleStr = @"I am a computer engineer. I wa"; NSLog(@"%d", [titleStr length]); if([titleStr length]>40 && [titleStr length]<=50){ [label setNumberOfLines:2]; [label setFont:[UIFont boldSystemFontOfSize:13]]; } else if([titleStr length]>30 && [titleStr length]<=40){ [label setNumberOfLines:2]; [label setFont:[UIFont boldSystemFontOfSize:16]]; } else if([titleStr length]>50){ [label setNumberOfLines:2]; [label setFont:[UIFont boldSystemFontOfSize:10]]; } else{ [label setFont:[UIFont boldSystemFontOfSize:20]]; [label setAdjustsFontSizeToFitWidth:YES]; } [label setText:titleStr]; self.navigationItem.titleView = label;