uilabel gets adaptive height (before and after iOS 7)

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

{

    if ([[[ UIDevice currentDevice ] systemVersion ] floatValue ] < 7 ) // Adaptive before 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;

        

    }


}



Guess you like

Origin blog.csdn.net/lichuanliangios/article/details/45787631