uilabel获得自适应高度(iOS 7 之前及之后)

-(float)getLabelHeight:(NSString *)str andFont:(CGFloat )font andWidth:(float)width

{

    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7 )//ios 7 之前自适应

    {

//        UIFont *font = [UIFont systemFontOfSize:font];

        CGSize maxSize=CGSizeMake(width, MAXFLOAT);

        

        CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:font] constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];

        

        

        return  size.height;

  

    }

    else

    {

        NSDictionary *dic=@{ NSFontAttributeName:[UIFont systemFontOfSize:font]};

        CGSize maxSize=CGSizeMake(width, MAXFLOAT);

        CGRect rect=[str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

        

        return rect.size.height;

        

    }


}



猜你喜欢

转载自blog.csdn.net/lichuanliangios/article/details/45787631
今日推荐