【问题标题】:How to add a multiline title bar in UINavigationController如何在 UINavigationController 中添加多行标题栏
【发布时间】:2012-10-10 17:25:53
【问题描述】:

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

【问题讨论】:

    标签: iphone ios ipad uinavigationcontroller titlebar


    【解决方案1】:

    只需设置setNumberOfLines: 0 of 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;
    

    设置左右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];
    

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

    这是根据其width设置fontSizeUILabel的方法。

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

    【讨论】:

    • 没有帮助我,因为我在导航栏中有一个左右按钮
    • 你应该在你的问题中提到你所有的问题。因为我不知道你的问题我只知道你问了什么。
    • 很抱歉没有描述所有内容。字符串长度从 5 到 60,因此字体大小必须灵活,标题不超过 2 行。我已经解决了固定字体大小的解决方案。我需要灵活的字体大小,例如 adjustsFontSizeToFitWidth 属性
    • 不错的解决方案。再次抱歉,这对我没有帮助,该行不应超过 2,导航标题的默认大小为 20,并且应该随着长度的增加而减小。
    • 你试过最后的编辑部分吗? ... 行数不会超过 2。
    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 2012-08-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      相关资源
      最近更新 更多