1

I want dynamic height of attribute text...
With my code some time I get perfect height but not all time... I am getting wrong the height when my html text size is too big... (It work perfect when my html content is less but not work when html content size is too big)

I have get size like below for html content attribute text

  1. I am convert my html string to attribute string in following way NSDictionary *fontDict = [NSDictionary dictionaryWithObject:fontRegular(13) forKey:NSFontAttributeName]; attrCoupon = [[NSMutableAttributedString alloc]initWithString: [NSString stringWithFormat:@"%@" ,_ObjCoupon.coupon_detail] attributes:fontDict];

  2. I am getting height of attribute string in following way CGRect rect = [attrCoupon boundingRectWithSize:(CGSize){self.tbl_details.frame.size.width , CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];

    • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGRect rect = [attrCoupon boundingRectWithSize:(CGSize){self.tbl_details.frame.size.width - 30, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];

      CGSize finalSize = rect.size; return finalSize.height ; }

4.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 NSAttributedString *attMerchnat1 = [[NSAttributedString alloc] initWithData:[_ObjCoupon.coupon_detail dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; CGRect rect = [attMerchnat boundingRectWithSize:(CGSize){self.tbl_details.frame.size.width, CGFLOAT_MAX}options:NSStringDrawingUsesLineFragmentOrigin context:nil]; cell.lblDetail.font = fontRegular(13); cell.lblDetail.attributedText = attMerchnat1; cell.lblDetail.frame = CGRectMake(10,3,cell.frame.size.width-20, rect.size.height); cell.lblDetail.numberOfLines = 0; cell.lblDetail.font = fontRegular(13); [cell.lblDetail sizeToFit]; 

I have tried all solution provided on stack but nothing works... I need help

4
  • When i removed the sizeToFit property i get space on top and bottom Commented Dec 19, 2015 at 5:33
  • I Have found Soluton... it definately will works Commented Dec 19, 2015 at 6:26
  • - (CGFloat)findHeightForText:(NSAttributedString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font { UITextView *textView = [[UITextView alloc] init]; [textView setAttributedText:text]; [textView setFont:font]; CGSize size = [textView sizeThatFits:CGSizeMake(widthValue, FLT_MAX)]; return size.height; } Commented Dec 19, 2015 at 6:26
  • Add above method and call on heightForRow at index path : int h = [self findHeightForText:attrCoupon havingWidth:SCREEN_WIDTH andFont:fontRegular(13)]; return h; Commented Dec 19, 2015 at 6:27

1 Answer 1

0

Please use this method .

- (CGSize)getSizeforController:(id)view{ UILabel *tempView =(UILabel *)view; NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init]; context.minimumScaleFactor = 0.8; float width = tempView.frame.size.width; CGSize size=[tempView.text boundingRectWithSize:CGSizeMake(width, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName : tempView.font} context:context].size; return size; } 

This might help .

Sign up to request clarification or add additional context in comments.

1 Comment

if you set the html text in the label and then pass the label to this method . is it not returning the expected height ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.