【问题标题】:iOS 9 Swift, UITextView Height based on the ContentiOS 9 Swift,基于内容的 UITextView 高度
【发布时间】:2016-09-02 02:21:27
【问题描述】:

我一直在尝试找到一种方法来调整整个地方的 UItextview 的高度,但找不到答案。所以,我请求你的帮助。我没有使用情节提要来进行约束。如何根据内容大小使 UITextView 的高度动态化?感谢您的帮助!我附上了一张图片来展示 textview 的样子。


enter image description here

 scrollView.addSubview(itemTitleView)
        itemTitleView.topAnchor.constraintEqualToAnchor(itemImage.bottomAnchor, constant: 80).active = true
        itemTitleView.widthAnchor.constraintEqualToAnchor(itemImage.widthAnchor).active = true
        itemTitleView.heightAnchor.constraintEqualToConstant(56).active = true
        itemTitleView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true

【问题讨论】:

  • 如果它是一个标签会更容易。因为它具有内在的高度和宽度。如果它不可编辑,您可以使用 UILabel 代替 textView。
  • 您的文本视图可编辑吗?还是仅用于展示目的?

标签: swift uitextview


【解决方案1】:

尝试将此扩展名用于字符串。如果您知道宽度是多少,这将为您提供所需的高度。你只需要宽度和字体就可以了。

extension String {
    func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: CGFloat.max)

        let boundingBox = self.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)

        return boundingBox.height
    }
}

【讨论】:

    猜你喜欢
    • 2018-09-07
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 2018-08-16
    • 2016-03-13
    • 1970-01-01
    • 2016-12-05
    相关资源
    最近更新 更多