如何获取 Swift 中 UILabel 的高度?

How to get the height of a UILabel in Swift?

我是 Swift 的初学者,我正在尝试获取标签的高度。 标签有多行文本。我想知道它在屏幕上占的总高度。

很简单,打电话就可以了

label.bounds.size.height

更新 Swift 3

func estimatedHeightOfLabel(text: String) -> CGFloat {

    let size = CGSize(width: view.frame.width - 16, height: 1000)

    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)

    let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 10)]

    let rectangleHeight = String(text).boundingRect(with: size, options: options, attributes: attributes, context: nil).height

    return rectangleHeight
}

override func viewDidLoad() {
    super.viewDidLoad()

    guard let labelText = label1.text else { return }
    let height = estimatedHeightOfLabel(text: labelText)
    print(height)
}

Swift 4 带扩展名

extension UILabel{

public var requiredHeight: CGFloat {
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width, height: CGFloat.greatestFiniteMagnitude))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.font = font
    label.text = text
    label.attributedText = attributedText
    label.sizeToFit()
    return label.frame.height
  }
}

@iajmeri43 的回答更新为 Swift 4

func estimatedLabelHeight(text: String, width: CGFloat, font: UIFont) -> CGFloat {

    let size = CGSize(width: width, height: 1000)

    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)

    let attributes = [NSAttributedStringKey.font: font]

    let rectangleHeight = String(text).boundingRect(with: size, options: options, attributes: attributes, context: nil).height

    return rectangleHeight
}

使用它(第 4 步):

// 1. get the text from the label
guard let theLabelsText = myLabel.text else { return }

// 2. get the width of the view the label is in for example a cell
// Here I'm just stating that the cell is the same exact width of whatever the collection's width is which is usually based on the width of the view that collectionView is in
let widthOfCell = self.collectionView.frame.width

// 3. get the font that your using for the label. For this example the label's font is UIFont.systemFont(ofSize: 17)
let theLabelsFont = UIFont.systemFont(ofSize: 17)

// 4. Plug the 3 values from above into the function
let totalLabelHeight = estimatedLabelHeight(text: theLabelsText, width: widthOfCell, font: theLabelsFont)

// 5. Print out the label's height with decimal values eg. 95.46875
print(totalLabelHeight)

// 6. as @slashburn suggested in the comments, use the ceil() function to round out the totalLabelHeight
let ceilHeight = ceil(totalLabelHeight)

// 7. Print out the ceilHeight rounded off eg. 95.0
print(ceilHeight)

Swift 5 ioS 13.2 tested 100%, best solution when the UILabel numberOfLines = 0

注意, 结果四舍五入。如果不需要,只需删除 ceil()

如果你想获得高度 -> 给出 UILabel 的故事板宽度

如果你想获得宽度 -> 给出 UILabel 的故事板高度

    let stringValue = ""//your label text
    let width:CGFloat = 0//storybord width of UILabel
    let height:CGFloat = 0//storyboard height of UILabel
    let font = UIFont(name: "HelveticaNeue-Bold", size: 18)//font type and size

    func getLableHeightRuntime() -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = stringValue.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return ceil(boundingBox.height)
    }

    func getLabelWidthRuntime() -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = stringValue.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return ceil(boundingBox.width)
    }