找出适合大小的 UILabel 的高度 [英] find out height of a UILabel which is size to fit

查看:21
本文介绍了找出适合大小的 UILabel 的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 UILabel 的高度动态变化时,即启用 sizeToFit 时,我试图找出 UILabel 的高度.计算 UILabel 高度的方法是什么?当然,当我启用sizeToFit时,frame.size.height的值显然是错误的.

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.

谢谢.

推荐答案

试试这个它可能对你有帮助..

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);

这篇关于找出适合大小的 UILabel 的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆