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

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

问题描述

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

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.

textview 的高度有点问题.

I have some troubles with the height of the textview.

有时,高度会根据文本完美调整.有时,不,文本在第一行被裁剪.下面,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.

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

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

我的 textview 的约束:

The constraints of my textview :

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

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

编辑

我尝试了@Nate 的建议,在 viewDidLoad 中,我添加了:

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

编辑 2

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

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]

推荐答案

我找到了解决方案.

我专注于 UITextView 的高度,然后我阅读了一篇关于纵横比的帖子.我想试一试,效果很好!

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.

在 viewDidLayoutSubviews() 中,我写道:

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

}

效果很好.

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

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