ios - 根据内容的大小调整文本标签的大小

标签 ios swift uitableview label

我读过几篇关于这个主题的文章:UILabel - auto-size label to fit text? , Swift - Adjusting fontSize to fit the width of the layout (programmatically) , 和 http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift .看来我完全按照这些帖子的建议行事,但似乎行不通。 以下是目前我的模拟器显示的内容: enter image description here

我已经正确地设置了约束,我认为并且我这样写了以下内容:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 150

}

然后我写道:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CathyTaskLogTableViewCell

        cell.messageLabel.sizeToFit()
        cell.messageLabel.text = "If you'd like to have more control over the way objects are synced, you can keep them in the local datastore until you are ready to save them yourself using saveInBackground. To manage the set of objects that need to be saved, you can again use a label. The fromPinWithName: method on PFQuery makes it easy to fetch just the objects you care about."

        return cell
    }

我已经将 lines 行设置为 0,如下所示:

enter image description here

对于 label,我设置了所有四个约束以及宽度和高度约束。我的约束是这样的:

enter image description here

但是,它并没有导致我的所有线条都显示出来。对于如何解决这个问题,有任何的建议吗?提前致谢!!!

最佳答案

您应该删除标签的高度并从标签的底部添加新的约束到表格 View 。这可能会解决您的问题。因为如果您添加标签的约束高度那么它将修复标签的高度如果您放置底部约束那么无论您的内容有多少,它都会从底部开始固定像素高度。

关于ios - 根据内容的大小调整文本标签的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34868643/

相关文章:

ios - 单击自定义 UITableView 部分标题调用 didSelectRow : on first row of the section

ios - 应用程序无法在第二个模拟器上运行?

ios - 将 uitabbar 实现到 uitableview

iphone - 分组 TableView 下的 UIButton

ios - 如何查看哪些 UICollectionView 单元格已滚动

ios - 图像及其属性列表

ios - 如何使用 Accelerate 中的线性插值

ios - 如何使用for循环在页面控件中打印数组(swift4)

ios - IQKeyboardManager 在 subview 中不工作

ios - Xcode 5.1 打破了一些测试