I have UILabel with dynamic height. But when my label has less characters then content of uilabel is aligned correctly. But if characters are more the content alignment changes. Please see following screenshots
As shown in above images text alignment is shifted down words. Here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *lognString = @"HAAAasfkjfg;jpgijopfadsgdfk;glsdfkls;gja;sjgasd;kjfgasod;fjals;dkfj kl;asddfjs;aipsdfj;asdjfs";
CGRect frame = label.frame;
label.numberOfLines = 0;
label.textAlignment = UITextAlignmentLeft;
label.lineBreakMode = UILineBreakModeWordWrap;
label.baselineAdjustment = UIBaselineAdjustmentNone;
frame.size.height = [self calculateHeightOfTextFromWidth:lognString font:[UIFont systemFontOfSize:20.0f] width:210.0 mode:UILineBreakModeWordWrap];
label.frame = frame;
label.text = lognString;
}
-(float) calculateHeightOfTextFromWidth:(NSString*)text font:(UIFont*)withFont width:(float)width mode:(UILineBreakMode)lineBreakMode
{
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
return suggestedSize.height;
}
Kindly ignore the text in UILable as this is a sample application Any help is greatly appreciated. Thanks