首页 > 解决方案 > 如何在 UITableViewCell 中计算 UILabel 正确的行号?

问题描述

对不起,我是 swift 的初学者。
首先,我有一个 UITableview 和一个动态 UITableViewCell。
我有一个关于在我的动态单元格中计算行号的问题。
我在 Stack 中找到了两个 UILabel 扩展答案,但它们对我不起作用。
对我有什么想法吗?
谢谢。



class ViewController: UIViewController {

let tableView = UITableView()
let cellNoButton = "cellNoButton"
let cellWithButton = "cellWithButton"
var isExpand: Bool = true
let text = "Students, who translate English texts, do exercises and do tests are very good at translating, doing exercises and doing tests, but they have problems with understanding English in real life. In real life, nobody waits for your translation. People usually use simple English when they speak but they use it fast. You have to understand with no translation to your native language. If you translate, you cannot be part of communication because you are thinking about the language too much. These words are maybe hard to read but they are true.So, if you want to understand English fast and learn fast, read two articles or more a day. You can improve your reading and listening quickly when you read easy English news. We will help you learn English fast and understand it. When you use this website every day, you can learn 3000 words which you need for communication with anybody in English."

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorInset = .zero
    tableView.estimatedRowHeight = 50
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.register(NoButtonTableViewCell.self, forCellReuseIdentifier: cellNoButton)
    tableView.register(WithButtonTableViewCell.self, forCellReuseIdentifier: cellWithButton)

    self.view.addSubview(tableView)

    tableView.snp.makeConstraints { (make) in
        make.top.left.right.bottom.equalToSuperview()
    }
}
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if isExpand {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellNoButton, for: indexPath) as! NoButtonTableViewCell
        cell.titleLabel.text = text //titleLabel.numberOfLines = 0
        print("===) \(cell.titleLabel.numberOfVisibleLines)") //it return 1.
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellWithButton, for: indexPath) as! WithButtonTableViewCell
        return cell
    }
}
}

extension UILabel {
var numberOfVisibleLines: Int {
    let textSize = CGSize(width: CGFloat(self.frame.size.width), height: CGFloat(MAXFLOAT))
    let rHeight: Int = lroundf(Float(self.sizeThatFits(textSize).height))
    let charSize: Int = lroundf(Float(self.font.pointSize))
    return rHeight / charSize
}

func calculateMaxLines() -> Int {
    let maxSize = CGSize(width: frame.size.width, height: CGFloat(Float.infinity))
    let charSize = font.lineHeight
    let text = (self.text ?? "") as NSString
    let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
    let linesRoundedUp = Int(ceil(textSize.height/charSize)) 
    return linesRoundedUp
}
}

标签: iosswiftuitableviewuilabel

解决方案


试用此代码以获取行数

extension UILabel {

    func calculateLines() -> Int {
        let maxSize = CGSize(width: frame.size.width, height: CGFloat(Float.infinity))
        let charSize = font.lineHeight
        let text = (self.text ?? "") as NSString
        let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil)
        let linesRoundedUp = Int(ceil(textSize.height/charSize)) 
        return linesRoundedUp
    }

}

推荐阅读