【问题标题】:Programmatically update UITextView height以编程方式更新 UITextView 高度
【发布时间】:2018-03-02 17:16:24
【问题描述】:

我正在尝试根据内容更新 UITextView 的高度。我见过this solution,但无法让它与我当前的代码一起工作(仍在快速学习)

我这样定义 UITextView:

    let eventDetailInfoTextBox : UITextView = {
    let textbox = UITextView()
    textbox.backgroundColor = UIColor.clear
    textbox.layer.borderWidth = 1
    textbox.layer.borderColor = ColorPallet.AppTertiaryColor.cgColor
    textbox.layer.cornerRadius = 10
    textbox.setNeedsDisplay()
    let contentSize = textbox.sizeThatFits(textbox.bounds.size)
    textbox.isEditable = false
    return textbox
}()

然后在 setupViews() 中添加子视图,并通过调用 setupEventDetailInfoTextBox() 定义其在视图中的位置

    fileprivate func setupEventDetailInfoTextbox() {

    print("Event Detail Info Text Box Height: \(eventDetailInfoTextBox.contentSize.height)")

    var frame = eventDetailInfoTextBox.frame
    frame.size.height = eventDetailInfoTextBox.contentSize.height
    eventDetailInfoTextBox.anchor(eventDetailMapView.bottomAnchor, left: self.leftAnchor, bottom: nil, right: self.rightAnchor, topConstant: 8, leftConstant: 10, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: frame.size.height)

}

对 .anchor 的调用基于通过 link 找到的 Lets Build That App 框架,并且基本上封装了来自 Xcode 的本地函数。我知道这很有效,并且在我的应用程序中反复重复使用了相同的功能。

print 语句的输出为 -8,由适合显示 1 行文本的高度表示(有时)。

如果我的文本超过 1 行,谁能明白为什么我的文本框拒绝变大?

我正在使用 IOS 10、Xcode 8 并使用 swift 3 编写代码。

【问题讨论】:

    标签: swift uitextview


    【解决方案1】:

    按照这些步骤进行

    • 创建TextView的高度约束出口。
    • 计算Textview的内容高度。
    • 更新 TextView 高度约束常量值。
    • 调用 layoutIfNeeded() 函数。

    我希望它会起作用。

    【讨论】:

    • 感谢 Sushil 的回答。我忘记添加的是我没有使用故事板进行布局(因此是 .anchor 代码)。我的部分问题与我在哪里执行您建议的任务有关。可以给点指导吗?
    【解决方案2】:

    如果您想要一些静态、低级和肮脏的东西,您可以简化流程(而不是设置和重置属性),只要您按正确的顺序放置所有内容。如果您需要处理设备旋转或其他运行时更改,那需要的东西会稍微少一些,但不会太多——我不确定您需要哪个,所以我选择了更简单的。

    // instance property so that other constraints can refer to it
    let descriptionTextView = UITextView()
    
    // configure text view
    descriptionTextView.text = descriptionModel
    descriptionTextView.font = // UIFont
    descriptionTextView.textColor = // UIColor
    descriptionTextView.isEditable = false
    descriptionTextView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(descriptionTextView)
    
    // set constraints
    descriptionTextView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
    descriptionTextView.topAnchor.constraint(equalTo: divider.bottomAnchor, constant: 32).isActive = true
    descriptionTextView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -32).isActive = true
    descriptionTextView.sizeToFit()
    descriptionTextView.layoutIfNeeded()
    descriptionTextView.heightAnchor.constraint(equalToConstant: descriptionTextView.contentSize.height).isActive = true
    

    【讨论】:

      猜你喜欢
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      相关资源
      最近更新 更多