【问题标题】:UITextView Changing Size DynamicallyUITextView 动态改变大小
【发布时间】:2016-09-03 18:04:29
【问题描述】:

我正在使用以下代码动态调整包含我的UITextViewcontainerView 的高度

    func textViewDidChange(textView: UITextView) {
    let amountOfLinesToBeShown:CGFloat = 6
    let maxHeight:CGFloat = textView.font!.lineHeight * amountOfLinesToBeShown

    if ((textView.contentSize.height / textView.font!.lineHeight) < 6) {
        topConstraint?.constant = -textView.sizeThatFits(CGSizeMake(textView.frame.size.width, maxHeight)).height - keyboardFrameSize!.height

        containerView.layoutIfNeeded()
        containerView.updateConstraints()
    }
}

问题是,当我打字时,它看起来像这样:

想要的效果是这样的:

【问题讨论】:

    标签: ios swift uitextview nslayoutconstraint uitextviewdelegate


    【解决方案1】:

    这篇文章的最佳答案解决了这个问题。 How do I size a UITextView to its content?

    下面的代码不是我的,它来自上面链接的答案(只是为了方便他人而发布):

    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height:CGFloat.max))
    var newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    textView.frame = newFrame;
    

    【讨论】:

      【解决方案2】:

      下面的代码可能是您遇到的这个问题的解决方案。

         func textViewDidChange(textView: UITextView) {
              var newFrame = textView.frame
              var minHeight = 40 // ENTER THE MIN HEIGHT OR THE INITIAL HEIGHT OF THE TEXTVIEW
              var maxHeight = 100 // ENTER THE MAX HEIGHT YOU WANT
              newFrame.size  = textView.sizeThatFits(CGSizeMake(newFrame.size.width, maxHeight)) 
      
          if newFrame.size.height > minHeight && newFrame.size.height < maxHeight {
              // Change the origin.y value which is 10 to your desired margin for top.
              textView.frame = CGRectMake(0, 10, textView.frame.size.width, newFrame.size.height)
          }
      }
      

      【讨论】:

        【解决方案3】:

        创建一个函数并将其添加到您的 viewDidLoad 方法中,尝试使用:

        self.addSubview(containerView)
        containerView.addSubview(textView)
        
        containerView.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor).active=true
        containerView.leftAnchor.constraintEqualToAnchor(self.leftAnchor).active=true
        
        textView.topAnchor.constraintEqualToAnchor(containerView.topAnchor, constant: 3).active=true
        textView.leftAnchor.constraintEqualToAnchor(containerView.leftAnchor, constant: 3).active=true
        textView.widthAnchor.constraintEqualToAnchor(containerView.widthAnchor).active=true
        textView.heightAnchor.constraintEqualToAnchor(containerView.heightAnchor).active=true
        

        【讨论】:

        • 我应该在我的代码之外执行此操作还是代替我的代码执行此操作?请记住,UITextView 高度应该在我输入时动态变化,将高度锚设置为 30 不会阻止这种情况吗?
        • 可以把heightAnchor作为containerView.heightAnchor。
        • 表现和以前一样
        • 如果 Lines 的数量大于 2 Lines,heightAnchor 容器视图是双倍的。
        【解决方案4】:

        使用 textview textContainerInset 方法来实现你想要的

        //insets are top, left, bottom, top..
        tv.textContainerInset = UIEdgeInsetsMake(0,0,0,0);
        

        设置此方法后,当textview行开始时,增加textview的高度以查找textview的下一行开始,

        使用方法:- TextViewDidChange

        【讨论】:

          猜你喜欢
          • 2013-08-14
          • 2012-07-23
          • 2018-11-16
          • 2011-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-25
          • 2013-02-15
          相关资源
          最近更新 更多