0

I have a problem with my label. I am resizing the font of the label using a slider but i have to maintain the number of lines of my label. for example i have 3 lines of text when i will resize it. it must maintains 3 lines only. but in my code when I resize the font the label's number of lines is not maintaining. Thanks for the help.

here is my code:

   float fontSize = self.sliderFont.value;

   self.lblQuotesForImg.font = [UIFont fontWithName:self.lblQuotesForImg.font.fontName size:fontSize];


   [self.lblQuotesForImg setLineBreakMode:NSLineBreakByWordWrapping];
   self.lblQuotesForImg.numberOfLines = 0;

   [self.lblQuotesForImg sizeToFit];
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
user3205472
  • 175
  • 1
  • 1
  • 7

2 Answers2

0
Use this code for find no of line for label

NSInteger oneLineHeight = [self findHeightForText:@"A" havingWidth:width andFont:font].height;
    NSInteger totalHeight = [self findHeightForText:txt havingWidth:width andFont:font].height;
    NSInteger noOfLines = totalHeight/oneLineHeight;

- (CGSize)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
    CGSize size = CGSizeZero;
    if (text) {
        //iOS 7
        CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{ NSFontAttributeName:font }
                                          context:nil];
        size = CGSizeMake(frame.size.width, frame.size.height + 1);
    }
    return size;
}
Rashad
  • 11,057
  • 4
  • 45
  • 73
Vidhi Patel
  • 603
  • 5
  • 10
0

Use this:

- (int)lineCountForText:(NSString *) text
{
    UIFont *font = [UIFont fontWithName:self.lblQuotesForImg.font.fontName size:fontSize];

    CGRect rect = [text boundingRectWithSize:CGSizeMake(200, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName : font}
                                     context:nil];

    return ceil(rect.size.height / font.lineHeight);
}

Hope this helps... :)

Rashad
  • 11,057
  • 4
  • 45
  • 73