Wednesday, 18 February 2015

iPhone -Get number of lines of UILabel


 First Option :

Firstly calculate height for text according to font :

NSInteger lineCount = 0;
CGSize labelSize = (CGSize){yourLabel.frame.size.width, MAXFLOAT};
CGRect requiredSize = [self boundingRectWithSize:labelSize  options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: yourLabel.font} context:nil];


Now calculate number of lines :
int charSize = lroundf(yourLabel.font.leading);
int rHeight = lroundf(requiredSize.height);
lineCount = rHeight/charSize;
NSLog(@"No of lines: %i",lineCount);



Second Option :

 NSInteger lineCount = 0;
 CGSize textSize = CGSizeMake(yourLabel.frame.size.width, MAXFLOAT);
 int rHeight = lroundf([yourLabel sizeThatFits:textSize].height)
 int charSize = lroundf(yourLabel.font.leading);
 lineCount = rHeight/charSize;
 NSLog(@"No of lines: %i",lineCount);

No comments:

Post a Comment