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

时间:2012-10-20 04:58:26

标签: iphone ios ipad uinavigationcontroller titlebar

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

2 个答案:

答案 0 :(得分:11)

只需设置setNumberOfLines:的{​​{1}} 0 即可。请参阅下面的示例。

UILabel

enter image description here

设置左右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; 的{​​{1}}。在这种情况下, 12 。 它看起来像这样:

Enter image description here

对于扩展问题:

只需在之前的代码中进行一些更改 -

FontSize

注意:您无法使用Label的{​​{1}}属性,因为在您需要处理[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]; 的情况下它不起作用它具有adjustsFontSizeToFitWidth:条件。

这是根据UILabel设置setNumberOfLines:0 if的方法。

fontSize

答案 1 :(得分:2)

感谢@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;