如何在UITableViewCell中计算UILabel正确的行号?

时间:2018-12-20 06:51:45

标签: ios swift uitableview 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
}
}

1 个答案:

答案 0 :(得分:0)

试用此代码以获取行数

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
    }

}