标签中的文本溢出屏幕 iOS Swift

text in label bleeding off screen iOS Swift

我在一个 table 单元格中添加了两个标签,顶部的标签左对齐,底部的标签右对齐,但是文本显示不正确。

我在故事板中为 table 视图添加了适当的约束,但这并没有解决问题。这是我用来设置标签的代码和生成的屏幕截图,我只是将边框用于调试目的

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)


    var label1 = UILabel(frame: CGRectMake(0,0, cell.bounds.width, cell.bounds.height/3))
    var label2 = UILabel(frame: CGRectMake(0,cell.bounds.height/3, cell.bounds.width, cell.bounds.height/3 * 2))



    label1.textAlignment = .Left
    label1.text = "AAAA"
    label1.layer.borderColor = UIColor.blackColor().CGColor
    label1.layer.borderWidth = 1.0


    label2.textAlignment = .Right
    label2.text = "BBBB"
    label2.layer.borderColor = UIColor.redColor().CGColor
    label2.layer.borderWidth = 1.0


    cell.addSubview(label1)
    cell.addSubview(label2)

编辑: 添加新图片以显示约束

您对 cell 本身没有任何限制。我建议您继承 UITableViewCell,将两个标签添加到自定义单元格,设置约束,然后在 tableView.dequeueReusableCellWithIdentifier 中修改这些标签。然后您将保留故事板中设置的约束。

由于当前存在,每次加载单元格时都会创建新的 labels,因此您还必须以编程方式创建约束,这并不理想。

固定所有边缘时移除 constraints to margin ..

有关保证金限制的更多指南,请查看此 link