【问题标题】:Self sizing uitextview till specific height自定尺寸uitextview直到特定高度
【发布时间】:2018-07-15 06:38:50
【问题描述】:

我有一个具有聊天功能的应用程序,其中UITextview 用于输入消息。 UITextview 高度必须是动态的(如果用户输入消息,高度必须根据文本长度更改直到特定高度)。

我怎样才能做到这一点?

【问题讨论】:

标签: ios swift uitextview


【解决方案1】:

禁用 textView 的滚动。

将高度增加到特定值,然后启用滚动。

提供最大高度限制然后将此代码添加到您的 viewController

 class YourViewController: UIViewController, UITextViewDelegate
    {
        @IBOutlet weak var yourTextView: UITextView!

        let textViewMaxHeight: CGFloat = 100
        override func viewDidLoad()
        {
            super.viewDidLoad()
            yourTextView.delegate = self
        }

        func textViewDidChange(textView: UITextView)
        {
            if textView.contentSize.height >= self.textViewMaxHeight
            {
                textView.scrollEnabled = true
            }
            else
                {
                textView.frame.size.height = textView.contentSize.height
                textView.scrollEnabled = false
            }
        }
    }

【讨论】:

  • 它没有回答 OP 的问题。禁用只会导致固定可见文本区域的高度。
  • 如何在 4 行后停止(textview 必须在 4 行后完成其原始功能)
  • @midhunnarayan 你是说最多 4 行它应该增加高度,然后,它应该启用滚动??
  • @Anuraj 是的
  • @Anuraj 试过,但在我的情况下,在这种情况下 (textView.contentSize.height >= self.textViewMaxHeight) textview height 变为 0 view 没有显示
【解决方案2】:

为你的 textView 添加一个高度约束并创建一个插座以便你可以调整它。然后可以使用 UIExfield 委托方法 textViewDidChange(_ textView: UITextView) 来调整高度。

func textViewDidChange(_ textView: UITextView) {

    // get the current height of your text from the content size
    var height = textView.contentSize.height

    // clamp your height to desired values
    if height > 90 {
        height = 90
    } else if height < 50 {
        height = 50
    }

    // update the constraint
    textViewHeightConstraint.constant = height
    self.view.layoutIfNeeded()
}

更短的版本...

func textViewDidChange(_ textView: UITextView) {
    let maxHeight: CGFloat = 90.0
    let minHeight: CGFloat = 50.0
    textViewHeightConstraint.constant = min(maxHeight, max(minHeight, textView.contentSize.height))           
    self.view.layoutIfNeeded()
}

【讨论】:

  • 确保将您的控制器设置为 textView 的委托。 textView.delegate = self in viewDidLoad 或在情节提要中设置。
  • 也没有其他约束应该限制你的 textView 的高度。垂直它应该只固定在底部并从高度约束中获取它的高度。
  • 在 maxheight 之后,如果我输入文本,它不会显示
  • 确保为 textview 启用滚动。它应该在最大高度之后滚动。
  • 这个问题的最佳答案
【解决方案3】:

如果您将 UITextView 子类化,这也可能是一个解决方案:

class Message: UITextView {


    var maximalSizeNotScrollable: CGFloat = 200
    var minimumHeightTextView: CGFloat = 35

    var textViewHeightAnchor: NSLayoutConstraint!


    override var contentSize: CGSize {
        didSet {

            if textViewHeightAnchor != nil {
                textViewHeightAnchor.isActive = false

            }
            textViewHeightAnchor = heightAnchor.constraint(equalToConstant: min(maximalSizeNotScrollable, max(minimumHeightTextView, contentSize.height)))
            textViewHeightAnchor.priority = UILayoutPriority(rawValue: 999)

            textViewHeightAnchor.isActive = true
            self.layoutIfNeeded()
        }
    }
}

优先级需要小于 1000,否则会遇到 _UITemporaryLayoutHeight 的问题。

【讨论】:

    【解决方案4】:

    我也在为同样的问题而苦苦挣扎。在阅读@Anuraj的回复后,我将 UITextView 的高度约束拖放到视图控制器,因为当 textview 达到最大高度并且启用滚动时,textview 变得越来越小只是为了显示一行,所以我找到了解决方案给textview 高度约束的最大高度。 Textview 将扩展直到达到最大高度,之后它将是可滚动的。删除文本时,文本视图高度会自动变小。

    @IBOutlet weak var txMessageHeight: NSLayoutConstraint!    
    let textViewMaxHeight: CGFloat = 120
    
    func textViewDidChange(_ textView: UITextView) {
        if textView.contentSize.height >= self.textViewMaxHeight{
            txMessageHeight.constant = self.textViewMaxHeight
            textView.isScrollEnabled = true
        }else{
            textView.frame.size.height = textView.contentSize.height
            textView.isScrollEnabled = false
        }
    }
    

    【讨论】:

      【解决方案5】:

      在 StoryBoard 中将 TextView 的高度约束设置为

      func textViewDidChange(_ textView: UITextView) {
              if textView.contentSize.height >= maxHeight {
                  textView.isScrollEnabled = true
              } else {
                  textView.isScrollEnabled = false
              }
          }
      

      如果你想在TextView为空时重置它的高度,你可以写

      UIView.animate(withDuration: 0.15) {
         self.view.layoutIfNeeded()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-24
        • 2016-05-20
        • 1970-01-01
        相关资源
        最近更新 更多