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

Iphone 如何在UINavigationController中添加多行标题栏,iphone,ios,ipad,uinavigationcontroller,titlebar,Iphone,Ios,Ipad,Uinavigationcontroller,Titlebar,我尝试在UINavigationController中添加两行标题栏 我想根据字符串长度自动调整字体大小。我的字符串最大大小为60。我试图通过以下代码实现 UILabel *bigLabel = [[UILabel alloc] init]; bigLabel.text = @"1234567890 1234567890 1234567890 1234567890 1234567890 123456"; bigLabel.backgroundColor = [UIColor clearColor

我尝试在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;
UILabel*bigLabel=[[UILabel alloc]init];
bigLabel.text=@“1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 12345676”;
bigLabel.backgroundColor=[UIColor clearColor];
bigLabel.textColor=[UIColor whiteColor];
bigLabel.font=[UIFont boldSystemFontOfSize:20];
bigLabel.adjustsFontSizeToFitWidth=是;
bigLabel.clipsToBounds=否;
bigLabel.numberOfLines=2;
bigLabel.textAlignment=([self.title length]<10?NSTextAlignmentCenter:NSTextAlignmentLeft);
[bigLabel sizeToFit];
self.navigationItem.titleView=bigLabel;

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

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

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;

设置左和右
UIBarButtonims
-您可以添加它们

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;
同时减小
标签的
FontSize
。在这种情况下,它是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];

感谢@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;
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(0.0,0.0320.0,44.0)];
[标签setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[标签setTextAlignment:NSTextAlignmentCenter];
NSString*titleStr1=@“1234567890 1234567890 1234567890 1234567890 1234567890 123 4568”;
NSString*titleStr=@“我是一名计算机工程师,我是一名工程师”;
NSLog(@“%d”,标题长度];
如果([标题栏长度]>40&[标题栏长度]30&[标题栏长度]50){
[label setNumberOfLines:2];
[label setFont:[UIFont boldSystemFontOfSize:10];
}
否则{
[label setFont:[UIFont boldSystemFontOfSize:20];
[标签设置AdjustsFontSizeToFitWidth:是];
}
[标签设置文字:标题栏];
self.navigationItem.titleView=标签;

没有帮助我,因为我在导航栏上有一个左右按钮,你应该在问题中提到你的所有问题。因为我不知道你的问题,所以我只知道你问了些什么。对不起,没有全部描述。字符串的长度从5到60不等,因此字体大小必须灵活,标题不得超过2行。我已经解决了固定字体大小的解决方案。我需要灵活的字体大小,如adjustsFontSizeToFitWidth propertynice解决方案。再次抱歉,这对我没有帮助。行不应该超过2,导航标题的默认大小是20,并且应该随着长度的增加而减小。您是否尝试过上一个编辑部分。。。行数将不超过2。
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;