UILabel 高度问题

来源:互联网 发布:ubuntu cpuz 编辑:程序博客网 时间:2024/06/05 18:26

    UILabel *lable = [[UILabelalloc]initWithFrame:CGRectMake(0,0, tableView.frame.size.width,10)];

    NSString *str = @"asdkjfhaeoufhafhwoefgoabfuiasoefuiawlyfawgfiulaw";

    lable.text = str;

    //0表示行数无数

    lable.numberOfLines = 0;

    //让字符串适应lable

    [lable sizeToFit];

    //文本高度

    CGFloat strHeight = lable.frame.size.height;

    [self.view addSubview:lable];

    [lable release];




//根据要显示的text计算label高度(封装起来)

- (CGFloat)contentCellHeightWithText:(NSString*)text

{

    NSInteger nowHeight;

    UIFont *font = [UIFontfontWithName:@"Arial"size:11];//11一定要跟label的显示字体大小一致

    //设置字体

    CGSize size =CGSizeMake(300,20000.0f);//注:这个宽:300是你要显示的宽度既固定的宽度,高度可以依照自己的需求而定

    if (SystemVersion_7x)//IOS 7.0以上

    {

        NSDictionary * tdic = [NSDictionarydictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];

        size =[text boundingRectWithSize:sizeoptions:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeadingattributes:tdiccontext:nil].size;

    }

    else

    {

        size = [text sizeWithFont:fontconstrainedToSize:sizelineBreakMode:NSLineBreakByCharWrapping];//ios7以上已经摒弃的这个方法

    }

    nowHeight = size.height;

    return nowHeight;

}


0 0