谁还没个明天 2009-06-28 08:54 采纳率: 50%
浏览 1004
已采纳

在 UILabel 内部垂直将文本对齐到顶部

I have a UILabel with space for two lines of text. Sometimes, when the text is too short, this text is displayed in the vertical center of the label.

How do I vertically align the text to always be at the top of the UILabel?

image representing a UILabel with vertically-centered text

转载于:https://stackoverflow.com/questions/1054558/vertically-align-text-to-top-within-a-uilabel

  • 写回答

27条回答 默认 最新

  • local-host 2009-06-28 10:36
    关注

    There's no way to set the vertical align on a UILabel, but you can get the same effect by changing the label's frame. I've made my labels orange so you can see clearly what's happening.

    Here's the quick and easy way to do this:

        [myLabel sizeToFit];
    

    sizeToFit to squeeze a label


    If you have a label with longer text that will make more than one line, set numberOfLines to 0 (zero here means an unlimited number of lines).

        myLabel.numberOfLines = 0;
        [myLabel sizeToFit];
    

    Longer label text with sizeToFit


    Longer Version

    I'll make my label in code so that you can see what's going on. You can set up most of this in Interface Builder too. My setup is a View Based App with a background image I made in Photoshop to show margins (20 points). The label is an attractive orange color so you can see what's going on with the dimensions.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // 20 point top and left margin. Sized to leave 20 pt at right.
        CGRect labelFrame = CGRectMake(20, 20, 280, 150);
        UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
        [myLabel setBackgroundColor:[UIColor orangeColor]];
    
        NSString *labelText = @"I am the very model of a modern Major-General, I've information vegetable, animal, and mineral";
        [myLabel setText:labelText];
    
        // Tell the label to use an unlimited number of lines
        [myLabel setNumberOfLines:0];
        [myLabel sizeToFit];
    
        [self.view addSubview:myLabel];
    }
    

    Some limitations of using sizeToFit come into play with center- or right-aligned text. Here's what happens:

        // myLabel.textAlignment = NSTextAlignmentRight;
        myLabel.textAlignment = NSTextAlignmentCenter;
    
        [myLabel setNumberOfLines:0];
        [myLabel sizeToFit];
    

    enter image description here

    The label is still sized with a fixed top-left corner. You can save the original label's width in a variable and set it after sizeToFit, or give it a fixed width to counter these problems:

        myLabel.textAlignment = NSTextAlignmentCenter;
    
        [myLabel setNumberOfLines:0];
        [myLabel sizeToFit];
    
        CGRect myFrame = myLabel.frame;
        // Resize the frame's width to 280 (320 - margins)
        // width could also be myOriginalLabelFrame.size.width
        myFrame = CGRectMake(myFrame.origin.x, myFrame.origin.y, 280, myFrame.size.height);
        myLabel.frame = myFrame;
    

    label alignment


    Note that sizeToFit will respect your initial label's minimum width. If you start with a label 100 wide and call sizeToFit on it, it will give you back a (possibly very tall) label with 100 (or a little less) width. You might want to set your label to the minimum width you want before resizing.

    Correct label alignment by resizing the frame width

    Some other things to note:

    Whether lineBreakMode is respected depends on how it's set. NSLineBreakByTruncatingTail (the default) is ignored after sizeToFit, as are the other two truncation modes (head and middle). NSLineBreakByClipping is also ignored. NSLineBreakByCharWrapping works as usual. The frame width is still narrowed to fit to the rightmost letter.


    Mark Amery gave a fix for NIBs and Storyboards using Auto Layout in the comments:

    If your label is included in a nib or storyboard as a subview of the view of a ViewController that uses autolayout, then putting your sizeToFit call into viewDidLoad won't work, because autolayout sizes and positions the subviews after viewDidLoad is called and will immediately undo the effects of your sizeToFit call. However, calling sizeToFit from within viewDidLayoutSubviews will work.


    My Original Answer (for posterity/reference):

    This uses the NSString method sizeWithFont:constrainedToSize:lineBreakMode: to calculate the frame height needed to fit a string, then sets the origin and width.

    Resize the frame for the label using the text you want to insert. That way you can accommodate any number of lines.

    CGSize maximumSize = CGSizeMake(300, 9999);
    NSString *dateString = @"The date today is January 1st, 1999";
    UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
    CGSize dateStringSize = [dateString sizeWithFont:dateFont 
            constrainedToSize:maximumSize 
            lineBreakMode:self.dateLabel.lineBreakMode];
    
    CGRect dateFrame = CGRectMake(10, 10, 300, dateStringSize.height);
    
    self.dateLabel.frame = dateFrame;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(26条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧