ios - 如何根据 SWIFT 中的内容调整 TextView 的高度?

标签 ios swift autolayout uitextview

在我的 View Controller 中,我有一个 UITextView。这个 TextView 填充了一个字符串。字符串可以很短也可以很长。根据字符串的长度, TextView 的高度必须进行调整。 我使用 Storyboard和自动布局。

我在 textview 的高度方面遇到了一些麻烦。

有时,高度会根据文本完美调整。有时,不,文本在第一行被裁剪。下面,textview 是黄色的。蓝色是 ScrollView 内的容器 View 。紫色是图片的位置。

title1 title2

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

my amazing new title

Another funny title for demo

我的 TextView 的限制:

enter image description here

我不明白为什么我有 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))

没有结果。在界面生成器中为我的 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]

最佳答案

我找到了解决方案。

我关注的是 UITextView 的高度,然后我阅读了一篇讨论宽高比的帖子。我想尝试一下,结果成功了!

所以在 Storyboard 中,我为 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!)

}

它完美地工作。

关于ios - 如何根据 SWIFT 中的内容调整 TextView 的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29431968/

相关文章:

ios - 具有多个 View Controller 的核心数据

ios - 是否可以在没有 Apple Developer Program 的情况下在 iOS 应用程序中使用 Firebase Cloud Messaging?

ios - 如何为自动调整单元格正确设置自动布局约束

ios - 在 iOS 上以小型大写字母排版字体

ios - 从字符串转换NSURL

ios - 比较两个单元格中的值

python - 像 swift 一样在 python 中进行类扩展?

iOS:使用 AutoLayout 调整 UIImageView 的大小

ios - 键盘盖住iAd,怎么修?

iPhone 移动应用程序和投票/Web 服务