Search code examples
iosswiftuiviewautolayoutnslayoutconstraint

Get height of UIView based on specific width before showing view


I have a UIView which I have designed in Interface Builder. It basically consists out of a header image and some text (UILabel) below. The view is being shown modally with a custom Transition and doesn't fill the whole screen.

There is like a 20 pixels margin on the left and right and 40 px on the top. The UILabel gets filled with some text that's coming from the web. What I want do do, is to find (or should I say predict) the height of the whole view for a specific width. How can I do that?


Solution

  • You need to have both the picture and the label before calculating the aspected size. I guess you should use something like this (maybe adding the vertical inter-distance between the imageView and the Label to the sum, and maybe removing the lateral margins from the width):

    objective C :

    - (CGFloat)preferredHeightFromWidth:(CGFloat)width text:(NSString *)text font:(UIFont *)font image:(UIImage *)image
    {
        // Calculate label height
        CGFloat labelHeight = [text
                               boundingRectWithSize:CGSizeMake(width, 10000)
                               options:NSStringDrawingUsesLineFragmentOrigin
                               attributes:@{NSFontAttributeName:font}
                               context:[[NSStringDrawingContext alloc] init]
                               ].size.height;
    
        // Calculate image height
        CGFloat ratio = image.size.height/ image.size.width;
        CGFloat imageHeight = (ratio * width);
    
        // Do the sum
        return labelHeight + imageHeight;
    }
    

    Swift:

    func preferredHeight(width: CGFloat, text: NSString, font: UIFont, image: UIImage) -> CGFloat {
        // Calculate Label Height
        let labelRect = text.boundingRect(
            with: CGSize.init(width: width, height: 10000),
            options: .usesLineFragmentOrigin,
            attributes: [NSFontAttributeName : font],
            context: NSStringDrawingContext())
        let labelHeight = labelRect.height
    
        // Calculate Image Height
        let ratio = image.size.height / image.size.width
        let imageHeight = ratio / width
    
        // Calculate Total Height
        let height = labelHeight + imageHeight
    
        // Return Height Value
        return height
    }
    

    (Thanks to Christopher Hannah for swift version)