thndrkiss
thndrkiss

Reputation: 4595

find out height of a UILabel which is size to fit

I am trying to find out the height of the UILabel when the height of the UILabel changes dynamically that is when it is sizeToFit enabled. What is the way to figure out the height of the UILabel? Of course the value frame.size.height is obviously wrong when I enable sizeToFit.

Thanks.

Upvotes: 0

Views: 239

Answers (2)

karthikeyan
karthikeyan

Reputation: 3898

try this it may be help you..

-(CGSize)lblSize:(NSString *)content lblFont:(UIFont *)contectLblFont width:(int)Width
{
    CGSize maximumLabelSize;
    maximumLabelSize = CGSizeMake(Width,9999);
    NSAttributedString *attributedText =
    [[NSAttributedString alloc]
     initWithString:content
     attributes:@
     {
     NSFontAttributeName:contectLblFont
     }];
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){maximumLabelSize.width, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;
    return size;
}

and use inside table view delegate

CGSize expectedLabelSize=[self lblSize:[NSString stringWithFormat:@"%@",[[loadMoreArray objectAtIndex:indexPath.row]valueForKey:@"text"]] lblFont:cell.self.lbldetail.font width:310];

    cell.self.lbldetail.frame=CGRectMake(5,cell.imgProfile.frame.size.height+cell.imgProfile.frame.origin.y+2, 300,expectedLabelSize.height+10);

Upvotes: 1

Jogendra.Com
Jogendra.Com

Reputation: 6454

I also using sizeToFit but getting Correct result try this may be helpfull. Plz try must.

 UILabel *detailsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,50,320,30)];
 detailsLabel.text = [textDic valueForKey:@"Text"];
 detailsLabel.textColor = [UIColor grayColor];
 detailsLabel.numberOfLines = 0;
 [detailsLabel sizeToFit];
 [scrollViewObj addSubview:detailsLabel];
 NSLog(@"Label Height %f",  detailsLabel.frame.size.height );

Every thing working fine in my current project .Thanks Cheers ..

Upvotes: 1

Related Questions