Anny
Anny

Reputation: 499

Get Dynamic Height of Label with attribute text (attribute text contains HTML String)

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

Upvotes: 1

Views: 849

Answers (1)

sourav
sourav

Reputation: 819

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 .

Upvotes: 0

Related Questions