gpt4 book ai didi

ios - 如何以编程方式更改 detailTextLabel 高度

转载 作者:可可西里 更新时间:2023-11-01 00:39:38 25 4
gpt4 key购买 nike

我将一个数组编程到 tableView 中。当单元格为 detailTextLabel 使用多行时,行之间的空间很小。我想知道是否有任何方法可以通过编程方式增加这个高度?这是我用于数组的示例代码。

cell.textLabel?.text = self.filtered[indexPath.row].coptic
cell.detailTextLabel?.text = self.filtered[indexPath.row].english
cell.textLabel?.font = UIFont(name:"CS Avva Shenouda", size:30)
cell.detailTextLabel?.font = UIFont(name: "Constantia", size:25)
cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0
cell.detailTextLabel?.textColor = UIColor.darkGray

return cell

最佳答案

我只是在表达我的逻辑,而不是整个代码。您可以通过以下代码获取字符串的高度

func height(withConstrainedWidth 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 ceil(boundingBox.height)
}

更改cellForRowAtIndexPath 方法

cell.detailTextLabel?.numberOfLines = 0 
let height = height(withConstrainedWidth:200, font:YourFont) // change width and font as per your requirement
cell.detailTextLabel?.frame = CGRect(x: cell.detailTextLabel?.frame.origin.x, y: cell.detailTextLabel?.frame.origin.y, width: cell.detailTextLabel?.frame.size.width, height: height)

可以根据detailTextLabel高度管理cell高度

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 50 // You should put your code or logic that dynamic height based on heigh of label.
}

关于ios - 如何以编程方式更改 detailTextLabel 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46741114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com