How to align UILabel text from bottom?

后端 未结 10 1504
盖世英雄少女心
盖世英雄少女心 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:43

    Subclass UILabel

    @interface Label : UILabel
    
    @end
    

    Then override drawTextInRect like so

    @implementation Label
    
    - (void)drawTextInRect:(CGRect)rect
    {
        if(alignment == top) {
    
            rect.size.height = [self sizeThatFits:rect.size].height;
        }
    
        if(alignment == bottom) {
    
            CGFloat height = [self sizeThatFits:rect.size].height;
    
            rect.origin.y += rect.size.height - height;
            rect.size.height = height;
        }
    
        [super drawTextInRect:rect];
    }
    
    @end
    

提交回复
热议问题