【问题标题】:How to calculate UILabel correct line number in UITableViewCell?如何在 UITableViewCell 中计算 UILabel 正确的行号?
【发布时间】:2019-05-20 16:23:56
【问题描述】:

抱歉,我是 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
}
}

【问题讨论】:

  • 计算动态单元格中的行号是什么意思?是否要获取单元格的行号
  • 为了什么目的需要行数,那有什么用?
  • 我想计算文本字符串是否超出两行。如果文本超出两行,我将使用带有“显示更多”按钮的收缩单元格,它可以展开并显示所有文本。
  • 为什么不用标签高度和字体大小来计算行数呢?我不知道 label.height / label.font.pointSize 是否正确,但我很确定如果你 floor 这个表达式你会得到行数。
  • @VladRusu 我在 Stack 中找到了这个。我只是拿来用。

标签: ios swift uitableview uilabel


【解决方案1】:

试用此代码获取行数

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
    }

}

【讨论】:

  • 对不起,我的代码已经有了这个。我的问题是打印(“===)(cell.titleLabel.numberOfVisibleLines)”)。并给我正确的行号,而不是动态单元格。
  • 添加扩展 calculateLines 以获取行数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 2017-01-25
  • 2016-06-23
  • 1970-01-01
  • 2017-03-16
相关资源
最近更新 更多