Figure out size of UILabel based on String in Swift

I am trying to calculate the height of a UILabel based on different String lengths.

func calculateContentHeight() -> CGFloat{
    var maxLabelSize: CGSize = CGSizeMake(frame.size.width - 48, CGFloat(9999))
    var contentNSString = contentText as NSString
    var expectedLabelSize = contentNSString.boundingRectWithSize(maxLabelSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(16.0)], context: nil)
    print("\(expectedLabelSize)")
    return expectedLabelSize.size.height

}

Above is the current function I use to determine the height but it is not working. I would greatly appreciate any help I can get. I would perfer the answer in Swift and not Objective C.

  • Xcode 9.0 simulators “disappeared”
  • How does one compare one image to another to see if they are similar by a certain percentage, on the iPhone?
  • how to use javascript in uiwebview to get the clicked button id?
  • iPhone 4 AVFoundation : Capture from front and rear cameras simultaneously
  • Drawing animation
  • Convert UIImage to NSData and convert back to UIImage in Swift?
  • 2 Solutions Collect From Internet About “Figure out size of UILabel based on String in Swift”

    Use an extension on String

    Swift 3

    extension String {
        func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
            let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
            let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
    
            return ceil(boundingBox.height)
        }
    
        func width(withConstraintedHeight height: CGFloat, font: UIFont) -> CGFloat {
            let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
            let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
    
            return ceil(boundingBox.width)
        }
    }
    

    and also on NSAttributedString (which is very useful at times)

    extension NSAttributedString {
        func height(withConstrainedWidth width: CGFloat) -> CGFloat {
            let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
            let boundingBox = boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, context: nil)
    
            return ceil(boundingBox.height)
        }
    
        func width(withConstrainedHeight height: CGFloat) -> CGFloat {
            let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
            let boundingBox = boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, context: nil)
    
            return ceil(boundingBox.width)
        }
    }
    

    Swift 4

    Just change the value for attributes in the extension String methods

    from

    [NSFontAttributeName: font]
    

    to

    [.font : font]
    

    For multiline text this answer is not working correctly.
    You can build a different String extension by using UILabel

    extension String {
    func height(constraintedWidth width: CGFloat, font: UIFont) -> CGFloat {
        let label =  UILabel(frame: CGRect(x: 0, y: 0, width: width, height: .greatestFiniteMagnitude))
        label.numberOfLines = 0
        label.text = self
        label.font = font
        label.sizeToFit()
    
        return label.frame.height
     }
    }
    

    The UILabel gets a fixed width and the .numberOfLines is set to 0. By adding the text and calling .sizeToFit() it automatically adjusts to the correct height.

    Code is written in Swift 3 πŸ”ΆπŸ¦