如何调整一个TextView他在SWIFT内容的高度? [英] How to adjust the height of a textview to his content in SWIFT?

查看:653
本文介绍了如何调整一个TextView他在SWIFT内容的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的视图控制器,我有一个UITextView的。此TextView的填充有字符串。该字符串可以是短期或长期。取决于串的长度,所述的TextView的高度必须调整。
我用故事板和经销商的布局。

我有一些麻烦与TextView的高度。

有时候,该高度是完全调整到文本。有时候,不,该文本被裁剪的第一行。下面,TextView的是黄色的。蓝色是滚动型内containerview。紫色是对图片的地方。

标题2

正文是从我的核心数据的基础上,他们是字符串属性。
屏幕1和2之间,唯一改变的是字符串。结果
如果我在控制台打印字符串我有正确的文本:

 我惊人的新头衔另一个有趣的标题演示

我的TextView的约束:

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

修改

我试过@Nate建议,在viewDidLoad中,我说:

  myTextViewTitle.text =我的惊人新标题    myTextViewTitle.setContentHuggingPriority(1000,forAxis:UILayoutConstraintAxis.Vertical)
    myTextViewTitle.setContentCom pressionResistancePriority(1000,forAxis:UILayoutConstraintAxis.Vertical)
    myTextViewTitle.setTranslatesAutoresizingMaskIntoConstraints(假)    让意见= [new_view:myTextViewTitle]
    VAR constrs = NSLayoutConstraint.constraintsWithVisualFormat(| -8- [new_view] -8- |选项:NSLayoutFormatOptions(0),指标:无,观点:观点)
    self.view.addConstraints(constrs)
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(V:| -8- [new_view],选项:NSLayoutFormatOptions(0),指标:无,观点:观点))
    self.myTextViewTitle.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(\"V:[new_view(220@300)]\",选项​​:NSLayoutFormatOptions(0),指标:无,观点:观点))

没有结果。带或不带高度约束实现增加了我在Interface Builder的TextView ...

编辑2

我需要一个UITextView的,而不是一个的UILabel,因为后面的按钮,使用排除路径。

 让exclusionPathBack:UIBezierPath = UIBezierPath(RECT:的CGRect(X:backButton.bounds.origin.x,Y:backButton.bounds.origin.y,宽度:backButton.bounds.width +10,身高:backButton.bounds.height))
     myTextViewTitle.textContainer.exclusionPaths = [exclusionPathBack]


解决方案

我找到了解决办法。

我专注于我的UITextView的高度,然后我看到一个帖子讲的纵横比。我想尝试并成功了!

所以在故事板,我添加了TextView的纵横比,我查在构建时删除选项。

在viewDidLayoutSubviews(),我写的:

 覆盖FUNC viewDidLayoutSubviews(){    super.viewDidLayoutSubviews()    让contentSize = self.myTextViewTitle.sizeThatFits(self.myTextViewTitle.bounds.size)
    VAR框架= self.myTextViewTitle.frame
    frame.size.height = contentSize.height
    self.myTextViewTitle.frame =框架    aspectRatioTextViewConstraint = NSLayoutConstraint(项目:self.myTextViewTitle,属性:.Height,relatedBy:.Equal,toItem:self.myTextViewTitle,属性:.WIDTH,事半功倍:myTextViewTitle.bounds.height / myTextViewTitle.bounds.width,恒:1)
    self.myTextViewTitle.addConstraint(aspectRatioTextViewConstraint!)}

它完美。

In my view controller, I have an UITextView. This textview is filled with a string. The string can be short or long. Depending on the length of the string, the height of the textview has to adjust. I use storyboards and auto layout.

I have some troubles with the height of the textview.

Sometimes, the height is perfectly adjusted to the text. Sometimes, no, the text is cropped on the first line. Below, the textview is yellow. the blue is a containerview inside a scrollview. Purple is the place for a picture.

The texts are from my core data base, they are string attributes. Between the screen 1 and 2, the only thing changed is the string.
If I print the strings in the console I have the correct texts :

my amazing new title

Another funny title for demo

The constraints of my textview :

I don't understand why I have 2 different displays.

EDIT

I tried the @Nate advice, in viewDidLoad, I added:

    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))

No results. With or without height contraint added for my textview in interface builder...

EDIT 2

I need an UITextView and not an UIlabel, because of the back button, to use an exclusion path.

     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]

解决方案

I found a solution.

I was focus on the height of my UITextView, then I read a post talking about the aspect ratio. I wanted to try and that worked!

So in the storyboard, I added an aspect ratio for the textview, and I checked "Remove at build time" option.

In viewDidLayoutSubviews(), I wrote :

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!)

}

It works perfectly.

这篇关于如何调整一个TextView他在SWIFT内容的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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