简体   繁体   English

以编程方式设置文本后获取UILabel的大小

[英]Get size of UILabel after setting text programmatically

When setting some string value to the text attribute of a UILabel like 将一些字符串值设置为UILabel的text属性时

var a : UILabel
a.text = "Variable length string"

autolayout resizes it using the constraints font size, label number of lines etc. autolayout使用约束字体大小,标签行数等来调整大小。

The question is, if I needed to get the size of that label so that I can ( for example ) decide how high a tablecell that includes this label should be, at what point should I do this successfully? 问题是,如果我需要获得该标签的大小以便我(例如)可以决定包含此标签的表格单元应该有多高,我应该在什么时候成功完成此操作?

Trying to figure this out without using UITableviewAutomaticDimension. 试图在不使用UITableviewAutomaticDimension的情况下解决这个问题。

EDIT : The answer posted as a possible duplicate bears some resemblence. 编辑 :作为可能重复发布的答案有一些相似之处。 However, my main confusion is as to where I can extract the height of the UILabel successfully and with certainty. 然而,我的主要困惑在于我能够成功且确定地提取UILabel的高度。

It has been many times that I have tried to get a view's size but the value I get back is not accurate and need to resort to using viewDidLayoutSubviews() in some form. 已经多次尝试获取视图的大小,但我得到的值不准确,需要采用某种形式使用viewDidLayoutSubviews() I guess I don't understand the order of which things happen in the layout very well. 我想我不明白布局中发生的事情顺序。 And it seems to be different for viewDidLoad() and awakeFromNib() too but I might be mistaken about this. 对于viewDidLoad()和awakeFromNib()似乎也有所不同,但我可能会对此有所误解。

If anyone could point me to the right direction in understanding this i would be grateful 如果有人能指出我理解这个方向的正确方向,我将不胜感激

Try implementing the following method in your ViewController with the tableView: 尝试使用tableView在ViewController中实现以下方法:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
             return HeightCalculator.height(with: text, inViewController: self)
}

Then add the following class: 然后添加以下类:

import UIKit

class HeightCalculator {
    let title: String
    let viewController: UIViewController

    class func height(with title: String, inViewController viewController: UIViewController) -> CGFloat {
        let calculator = HeightCalculator(title: title, viewController: viewController)

        return calculator.height
    }

    init(title: String, viewController: UIViewController) {
        self.title = title
        self.viewController = viewController
    }

    var height: CGFloat {
        let contentHeight = title.heightWithConstrainedWidth(width: defaultWidth, font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.title1))
    }
}

You need the following extension on your String to get the height: 您需要在String上使用以下扩展名来获取高度:

import UIKit

extension String {
    func heightWithConstrainedWidth(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 boundingBox.height
    }
}

Use heightForRowAt to calculate the height, the calculator class calculating it (contentHeight) and the extension to get the height. 使用heightForRowAt计算高度,计算它的计算器类(contentHeight)和获得高度的扩展。 You can adjust the calculator class to pass the font in so it can use this font to pass it to the heightWithConstrainedWidth method. 您可以调整计算器类以传递字体,以便它可以使用此字体将其传递给heightWithConstrainedWidth方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM