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
  • 21,946
  • 10
  • 78
  • 104
user3205472
  • 165
  • 1
  • 6

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
  • 10,839
  • 4
  • 43
  • 71
Vidhi Patel
  • 583
  • 4
  • 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
  • 10,839
  • 4
  • 43
  • 71