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

标签 ios swift uitableview uilabel height

我将一个数组编程到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高度管理单元格高度

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/

相关文章:

ios - 如何将自定义类 UIButton 与同一类的另一个对象设置?

swift - setEditing 方法删除按钮不起作用

swift - SpriteBuilder | Cocos2d - swift |代码 | ccPhysicsCollisionBegin

ios - 如何追加到数组的一部分? [[]]

ios - 如何使用户从 twitter fabric ios 注销

16 进制的 Swift 科学记数法

ios - 使用 Tableview 告诉 ios 中的用户最近对服务器的调用没有获取任何数据

ios - 如何获得 map 上的中心位置和可见半径?

ios - 在 iOS 中调用多个服务的更好方法是什么?

ios - iTunes Connect 中缺少预发布版