ios - 如何在 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
}
}
解决方案
试用此代码以获取行数
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
}
}
推荐阅读
- jquery - 在 dragend 的弹出窗口中获取地址
- html - 应用溢出隐藏时删除小边框
- gradle - 无法独立导入 grails 插件:9.0.0.M4
- logging - kubernetes:kubectl 只流式传输一定数量的日志
- macos - 在 macOS 10.8 (Mountain Lion) 之后已弃用的资源 'CurResfile'、'UseResFile' 等的替代 API
- c# - 如何创建一个行数并不总是相同的滚动列表?
- c# - 为什么编译器与继承中的命名空间冲突
- scala - 如何从配置文件在数据框中添加架构
- r - 使用 R 生成用于情感分析的表格和图表
- python-3.x - 有没有办法在不同的数组之间共享一个变量?