UITextView 不在 nextLine 上打印文本? [英] UITextView doesn't print text on the nextLine?

查看:28
本文介绍了UITextView 不在 nextLine 上打印文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 UITextView 放在 UIView 中.UIView 随着用户在 UITextView 中键入而扩展,但问题是,如果用户在下一行键入,则在用户键入第三行之前它不会显示正在键入的文本,然后它会显示打印在第二行.第 3 行和第 4 行等也是如此.

I put the UITextView inside a UIView. The UIView expands as the user types in the UITextView but the problem is that if the user types on the next line, it doesn't show the text being typed until the user types on the third line, then it shows the text printed on the second line. Same goes with the 3rd line and 4th line, etc.

我该如何解决这个问题?

How can I fix this?

func textViewDidChange(_ textView: UITextView) {
    print(textView.text)
    let size = CGSize(width: prayerRequest.frame.width, height: .infinity)

    let estimatedSize = textView.sizeThatFits(size)
    textView.constraints.forEach { (constraints) in
        if constraints.firstAttribute == .height {
            constraints.constant = estimatedSize.height

        }

        viewContainer.constraints.forEach({ (constraints) in
            if constraints.firstAttribute == .height {
                constraints.constant = estimatedSize.height
                viewContainer.layoutIfNeeded()
            }
        })
    }

}

推荐答案

如果您使用的是界面构建器,请尝试将行数设置为 0.

If you're using interface builder try setting the number of lines to 0.

或者从代码 textView.textContainer.maximumNumberOfLines = 10

这篇关于UITextView 不在 nextLine 上打印文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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