How to align UILabel text from bottom?

后端 未结 10 1498
盖世英雄少女心
盖世英雄少女心 2021-02-03 19:08

How the UILabel can be aligned from bottom. Let say, my label can hold three line of text.If the input text is single line, then this line should come bottom of the

10条回答
  •  攒了一身酷
    2021-02-03 19:49

    I had the same issue. Here is how I made to align the text to the bottom of my UILabel:

    - (void)alignLabel:(UILabel *)l withText:(NSString *)text verticalAlignOption:(int)vertAlign{
    CGSize stringSize = [text sizeWithFont:l.font constrainedToSize:l.frame.size lineBreakMode:l.lineBreakMode];
    
    switch (vertAlign) {
        case 0: // align = top
            l.frame = CGRectMake(l.frame.origin.x,
                                       l.frame.origin.y,
                                       l.frame.size.width,
                                       stringSize.height
                                       );
            break;
        case 1: // align = bottom
            l.frame = CGRectMake(l.frame.origin.x,
                                       (l.frame.origin.y + l.frame.size.height) - stringSize.height,
                                       l.frame.size.width,
                                       stringSize.height
                                       );
            break;
        case 2: // align = middle
            // default
            break;
    }
    
    l.text = text;
    }
    

    Ahd you simple call the method like this to align to the bottom:

    [self alignLabel:self.mediaTitle withText:@"My text to align" verticalAlignOption:1];
    

提交回复
热议问题