Figure out size of UILabel based on String in Swift

后端 未结 11 1416
执笔经年
执笔经年 2020-11-22 13:35

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

func calculateContentHeight() -> CGFloat{
    var maxLabelSize: CGSize         


        
相关标签:
11条回答
  • 2020-11-22 14:14
    @IBOutlet weak var constraintTxtV: NSLayoutConstraint!
    func TextViewDynamicallyIncreaseSize() {
        let contentSize = self.txtVDetails.sizeThatFits(self.txtVDetails.bounds.size)
        let higntcons = contentSize.height
        constraintTxtV.constant = higntcons
    }
    
    0 讨论(0)
  • 2020-11-22 14:16

    This solution will help to calculate the height and width at runtime.

        let messageText = "Your Text String"
        let size = CGSize.init(width: 250, height: 1000)
        let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
        let estimateFrame = NSString(string: messageText).boundingRect(with:  size, options: options, attributes: [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue", size: 17)!], context: nil)
    

    Here you can calculate the estimated height that your string would take and pass it to the UILabel frame.

    estimateFrame.Width
    estimateFrame.Height 
    
    0 讨论(0)
  • 2020-11-22 14:17

    In Swift 5:

    label.textRect(forBounds: label.bounds, limitedToNumberOfLines: 1)
    

    btw, the value of limitedToNumberOfLines depends on your label's text lines you want.

    0 讨论(0)
  • 2020-11-22 14:18

    Swift 5:

    If you have UILabel and someway boundingRect isn't working for you (I faced this problem. It always returned 1 line height.) there is an extension to easily calculate label size.

    extension UILabel {
        func getSize(constrainedWidth: CGFloat) -> CGSize {
            return systemLayoutSizeFitting(CGSize(width: constrainedWidth, height: UIView.layoutFittingCompressedSize.height), withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel)
        }
    }
    

    You can use it like this:

    let label = UILabel()
    label.text = "My text\nIs\nAwesome"
    let labelSize = label.getSize(constrainedWidth:200.0)
    

    Works for me

    0 讨论(0)
  • 2020-11-22 14:25

    Heres a simple solution thats working for me... similar to some of the others posted, but it doesn't not include the need for calling sizeToFit

    Note this is written in Swift 5

    let lbl = UILabel()
    lbl.numberOfLines = 0
    lbl.font = UIFont.systemFont(ofSize: 12) // make sure you set this correctly 
    lbl.text = "My text that may or may not wrap lines..."
    
    let width = 100.0 // the width of the view you are constraint to, keep in mind any applied margins here
    
    let height = lbl.systemLayoutSizeFitting(CGSize(width: width, height: UIView.layoutFittingCompressedSize.height), withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel).height
    

    This handles line wrapping and such. Not the most elegant code, but it gets the job done.

    0 讨论(0)
  • 2020-11-22 14:27
    extension String{
    
        func widthWithConstrainedHeight(_ height: CGFloat, font: UIFont) -> CGFloat {
            let constraintRect = CGSize(width: CGFloat.greatestFiniteMagnitude, height: height)
    
            let boundingBox = self.boundingRect(with: constraintRect, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
    
            return ceil(boundingBox.width)
        }
    
        func heightWithConstrainedWidth(_ width: CGFloat, font: UIFont) -> CGFloat? {
            let constraintRect = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
            let boundingBox = self.boundingRect(with: constraintRect, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
    
            return ceil(boundingBox.height)
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题