UITextView的高度可以在键入/不使用情节提要时动态更改 [英] UITextView height to change dynamically when typing / without using storyboards

查看:159
本文介绍了UITextView的高度可以在键入/不使用情节提要时动态更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个UITextView&我希望它的高度在用户键入内容时动态变化.我想以编程方式进行操作.我在另一个UIView上方有UITextView.约束设置如下:

I have this UITextView & I want it's height to change dynamically when the user is typing on it. I want to do it programmatically. I have the UITextView above another UIView. The constraints are set as below:

 addtextview.leadingAnchor.constraint(equalTo: addtasktextview.leadingAnchor, constant: 8).isActive = true
      addtextview.trailingAnchor.constraint(equalTo: addtasktextview.trailingAnchor, constant: -8).isActive = true
      addtextview.topAnchor.constraint(equalTo: addtasktextview.topAnchor, constant: 8).isActive = true
      addtextview.bottomAnchor.constraint(equalTo: addtasktextview.bottomAnchor, constant: -40).isActive = true

有趣的是,我也在tableViewCells中使用textview,并仅使用此约束方法来动态更改高度,但是在这里它不起作用. 我希望textview的高度以向上移动的方式增加.因此,当新行开始时,顶部应移动并保持下面的间距.

Interestingly enough, I am using textview in tableViewCells as well and dynamically changing the height by using just this constraint method, but over here it is not working. I want the textview's height to increase in such a way that it moves upward. So when a new line starts, the top part should move maintaining the spacing below.

我该怎么办?帮助将不胜感激.

How can I do it? Help will be appreciated it.

更新:我能够使用@ upholder-of-truth的下面的答案来使其工作.通过找到textview正常高度和newSize.height之间的差异,然后将该差异添加到容器的高度中,我还能够动态更改父UIView容器的高度.

UPDATE: I was able to get it working with @upholder-of-truth 's answer below. I was also able to dynamically change the parent UIView container height by finding the difference between the textview normal height and the newSize.height and then adding that difference to the container's height.

推荐答案

首先请确保您的类采用UITextViewDelegate协议,以便在文本更改如下时得到通知:

First make sure your class adopts the UITextViewDelegate protocol so you can be informed when the text changes like this:

class MyClass: UIViewContoller, UITextViewDelegate

接下来,在类的某个位置定义此变量,以便您可以跟踪约束中的高度:

Next define this variable somewhere in your class so that you can keep track of the height in a constraint:

var textHeightConstraint: NSLayoutConstraint!

接下来添加以下约束并激活它:

Next add the following constraint and activate it:

    self.textHeightConstraint = addtextview.heightAnchor.constraint(equalToConstant: 40)
    self.textHeightConstraint.isActive = true

(如果您不在viewDidLoad中执行此操作,则需要将textHeightConstraint设为可选)

(if you don't do this in viewDidLoad you need to make textHeightConstraint an optional)

下一步订阅委托(如果尚未完成):

Next subscribe to the delegate (if not already done):

addTextView.delegate = self

添加此函数以重新计算高度约束:

Add this function which recalculates the height constraint:

func adjustTextViewHeight() {
    let fixedWidth = addtextview.frame.size.width
    let newSize = addtextview.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    self.textHeightConstraint.constant = newSize.height
    self.view.layoutIfNeeded()
}

下一步,在创建约束以设置初始大小之后,添加对该函数的调用:

Next add a call to that function after the constraints are created to set the initial size:

self.adjustTextViewHeight()

最后添加此方法以在文本更改时调整高度:

Finally add this method to adjust the height whenever the text changes:

func textViewDidChange(_ textView: UITextView) {
    self.adjustTextViewHeight()
}

以防万一,这是UIViewController子类中的一个最小示例:

Just in case that is all confusing here is a minimal example in a sub class of a UIViewController:

class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet var textView: UITextView!
    @IBOutlet var textHolder: UIView!

    var textHeightConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        textView.leadingAnchor.constraint(equalTo: textHolder.leadingAnchor, constant: 8).isActive = true
        textView.trailingAnchor.constraint(equalTo: textHolder.trailingAnchor, constant: -8).isActive = true
        textView.topAnchor.constraint(equalTo: textHolder.topAnchor, constant: 8).isActive = true
        textView.bottomAnchor.constraint(equalTo: textHolder.bottomAnchor, constant: -40).isActive = true

        self.textHeightConstraint = textView.heightAnchor.constraint(equalToConstant: 40)
        self.textHeightConstraint.isActive = true

        self.adjustTextViewHeight()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textViewDidChange(_ textView: UITextView) {
        self.adjustTextViewHeight()
    }

    func adjustTextViewHeight() {
        let fixedWidth = textView.frame.size.width
        let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
        self.textHeightConstraint.constant = newSize.height
        self.view.layoutIfNeeded()
    }
}

这篇关于UITextView的高度可以在键入/不使用情节提要时动态更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆