【问题标题】:How to get content height of UITextView with word wrapping如何使用自动换行获取 UITextView 的内容高度
【发布时间】:2016-07-10 12:21:12
【问题描述】:

我正在尝试获取 UITextView 的内容高度以计算它的底部位置(y 位置 + 高度)。 我可以很好地获得 y 位置

travelDescriptionTextView.frame.origin.y

并且需要确定高度。在整个互联网上,它都说要通过以下方式获取内容高度:

let contentSize = self.travelDescriptionTextView.sizeThatFits(self.travelDescriptionTextView.bounds.size)
let height = contentSize.height

但是当高度通过自动换行调整(扩展)时,即如果句子比文本框的宽度更宽并且文本框自动创建新行,则此技术不起作用。上述获取内容高度的技术只有在没有自动换行的情况下才能获得正确的高度,否则高度不包括额外的自动换行行,导致内容高度小于实际内容高度。

那么如何获取包含自动换行高度调整大小的 UITextView 的内容高度?

【问题讨论】:

标签: ios swift height uitextview


【解决方案1】:

尝试如下,

let textViewComputedHeight = textView.contentSize.height -  textView.contentInset.top - textView.contentInset.bottom

【讨论】:

    【解决方案2】:

    感谢 RDC 提供链接。工作解决方案是:

                self.travelDescriptionTextView.sizeToFit()
                self.travelDescriptionTextView.layoutIfNeeded()
                let contentSize = self.travelDescriptionTextView.sizeThatFits(self.travelDescriptionTextView.bounds.size)
                let textViewHeight = contentSize.height
    

    【讨论】:

      猜你喜欢
      • 2018-08-16
      • 1970-01-01
      • 2011-11-03
      • 2016-11-26
      • 2014-05-28
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多