如何在Swift中获得UILabel的高度?

时间:2016-04-08 20:38:02

标签: swift uilabel

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

5 个答案:

答案 0 :(得分:10)

很简单,只需致电

label.bounds.size.height

答案 1 :(得分:10)

针对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)
}

enter image description here

答案 2 :(得分:8)

快速扩展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
  }
}

答案 3 :(得分:1)

Swift 5 ioS 13.2 测试了100%,这是 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)
    }

答案 4 :(得分:0)

@ 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)