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
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
i only set a bottom constraint to the super view in IB which works for me without using code and also number of Lines for a maximum constraint.
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];
Here are two ways of doing that...
1. First set numberOfLines
to 0 and then use sizeToFit
property of UILabel
so your UILabel
display with its contentSize
.
yourLabel.numberOfLines = 0;
[yourLabel sizeToFit];
See more information from this link: Vertically align text within a UILabel
2. Another option is to take UITextField
instead of UILabel
and set userInteractionEnabled
to NO
like below...
[yourTextField setUserInteractionEnabled:NO];
and then set the contentVerticalAlignment
property to bottom like below...
[yourTextField setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
UPDATE
Also, with UITextField
, we can't achieve multiple lines. So instead we can use UITextView
and set its userInteractionEnabled
to NO
. Then, use the code below to make it bottom aligned.
CGFloat topCorrect = ([label bounds].size.height - [label contentSize].height);
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
label.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
In IB as @Tobe said Bottom constraint to superview should work, Incase if you have multiple subview or horizontal stack view with one element to have bottom constraint then use Layout Margin to be Fixed with bottom less than other margins
I recently ran into this problem and was able to solve it by putting my label in a stackview by itself. I got the idea from this post which had the same question but with multiple labels. The same technique can be used with a single label.
The stackview would have axis = horizontal and alignment = bottom (which is what does the trick).
My label is now perfectly aligned towards the bottom which is what I needed.