如何在SWIFT中将textview的高度调整为他的内容?

cmi*_*mii 24 uitextview ios autolayout swift

在我的视图控制器中,我有一个UITextView.此textview填充了一个字符串.字符串可以是短的或长的.根据字符串的长度,textview的高度必须调整.我使用故事板和自动布局.

我对textview的高度有些麻烦.

有时,高度完全适应文本.有时,不,文本在第一行被裁剪.下面,textview是黄色的.蓝色是滚动视图中的容器视图.紫色是一张照片的地方.

TITLE1 标题2

文本来自我的核心数据库,它们是字符串属性.在屏幕1和屏幕2之间,唯一改变的是字符串.
如果我在控制台中打印字符串,我有正确的文本:

my amazing new title

Another funny title for demo
Run Code Online (Sandbox Code Playgroud)

我的textview的限制:

在此输入图像描述

我不明白为什么我有2个不同的显示器.

编辑

我尝试了@Nate建议,在viewDidLoad中,我补充说:

    myTextViewTitle.text="my amazing new title"

    myTextViewTitle.setContentHuggingPriority(1000, forAxis: UILayoutConstraintAxis.Vertical)
    myTextViewTitle.setContentCompressionResistancePriority(1000, forAxis: UILayoutConstraintAxis.Vertical)
    myTextViewTitle.setTranslatesAutoresizingMaskIntoConstraints(false)  

    let views = ["new_view": myTextViewTitle]
    var constrs = NSLayoutConstraint.constraintsWithVisualFormat("|-8-[new_view]-8-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views)
    self.view.addConstraints(constrs)
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[new_view]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    self.myTextViewTitle.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[new_view(220@300)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
Run Code Online (Sandbox Code Playgroud)

没有结果.在界面构建器中为我的textview添加或不添加高度约束...

编辑2

我需要一个UITextView而不是UIlabel,因为后退按钮,使用排除路径.

     let exclusionPathBack:UIBezierPath = UIBezierPath(rect: CGRect(x:backButton.bounds.origin.x, y:backButton.bounds.origin.y, width:backButton.bounds.width+10, height:backButton.bounds.height))
     myTextViewTitle.textContainer.exclusionPaths=[exclusionPathBack]
Run Code Online (Sandbox Code Playgroud)

cmi*_*mii 40

我找到了解决方案.

我专注于我的UITextView的高度,然后我读了一篇关于宽高比的帖子.我想试试这个有用!

所以在故事板中,我为textview添加了宽高比,并选中了"在构建时删除"选项.

在viewDidLayoutSubviews()中,我写道:

override func viewDidLayoutSubviews() {

    super.viewDidLayoutSubviews()

    let contentSize = self.myTextViewTitle.sizeThatFits(self.myTextViewTitle.bounds.size)
    var frame = self.myTextViewTitle.frame
    frame.size.height = contentSize.height
    self.myTextViewTitle.frame = frame

    aspectRatioTextViewConstraint = NSLayoutConstraint(item: self.myTextViewTitle, attribute: .Height, relatedBy: .Equal, toItem: self.myTextViewTitle, attribute: .Width, multiplier: myTextViewTitle.bounds.height/myTextViewTitle.bounds.width, constant: 1)
    self.myTextViewTitle.addConstraint(aspectRatioTextViewConstraint!)

}
Run Code Online (Sandbox Code Playgroud)

它完美地运作.


Сер*_*лык 22

首先,禁用UITextView的可滚动.两种选择:

  1. 取消选中.xib中的滚动启用.
  2. [TextView setScrollEnabled:NO];

创建一个UITextView并将其与IBOutlet(TextView)连接.添加具有默认高度的UITextView高度约束,将其与IBOutlet(TextViewHeightConstraint)连接.当您异步设置UITextView的文本时,您应该计算UITextView的高度并将UITextView的高度约束设置为它.示例代码:

CGSize sizeThatFitsTextView = [TextView sizeThatFits:CGSizeMake(TextView.frame.size.width, MAXFLOAT)];

TextViewHeightConstraint.constant = sizeThatFitsTextView.height; 
Run Code Online (Sandbox Code Playgroud)

  • 应该注意的是,如果您使用自动布局,则在故事板中选择"滚动已启用"会影响约束的布局方式,即使它没有明确显示该关系.我在使用autolayout约束垃圾时遇到困难,直到我取消勾选复选框.在此之后,自动布局完美地解决了. (3认同)

agr*_*ppa 8

在Swift 3.0中,tvHeightConstraint是主题TextView的高度约束(如果用于多个textview,则会将其添加到函数输入中):

func textViewDidChange(textView: UITextView) {

        let size = textView.sizeThatFits(CGSize(width: textView.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
        if size.height != tvHeightConstraint.constant && size.height > textView.frame.size.height{
            tvHeightConstraint.constant = size.height
            textView.setContentOffset(CGPoint.zero, animated: false)
        }
    }
Run Code Online (Sandbox Code Playgroud)

H/T到@cmi和@Pablo Ruan


Pab*_*uan 5

在swift 2.3中:

 func textViewDidChange(textView: UITextView) {

    let size = textView.sizeThatFits(CGSize(width: textView.frame.size.width, height: CGFloat.max))
    if size.height != TXV_Height.constant && size.height > textView.frame.size.height{
        TXV_Height.constant = size.height
        textView.setContentOffset(CGPointZero, animated: false)
    }
}
Run Code Online (Sandbox Code Playgroud)