1
votes

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

1
When i removed the sizeToFit property i get space on top and bottom - Anny
I Have found Soluton... it definately will works - Anny
- (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; } - Anny
Add above method and call on heightForRow at index path : int h = [self findHeightForText:attrCoupon havingWidth:SCREEN_WIDTH andFont:fontRegular(13)]; return h; - Anny

1 Answers

0
votes

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 .