【问题标题】:Resize TextView Heigh AND Width according to text - Swift根据文本调整 TextView 的高度和宽度 - Swift
【发布时间】:2019-01-06 23:05:47
【问题描述】:

我正在尝试使用 TableViewController 中的以下函数在 TableView 自定义单元格中自动调整 UITextView 的大小。它完美地工作:它根据文本长度调整我的 TextView 高度但我找不到关于如何调整 TextView 宽度的任何帮助!我已经将约束设置为将其限制在单元格的宽度上,但不起作用。有什么功能可以帮助我将 textView 宽度调整为高度? 谢谢。

this is my output now

func adjustUITextViewHeight(arg : UITextView)
    {
        arg.translatesAutoresizingMaskIntoConstraints = true
        arg.sizeToFit()
        arg.isScrollEnabled = false
    }

【问题讨论】:

    标签: ios swift textview resize width


    【解决方案1】:

    我用这个函数解决了:

    func adjustUITextViewHeight(arg : UITextView, viewBox : UIView)
    {
        arg.isScrollEnabled = false
        let size = CGSize(width: viewBox.frame.width, height: .infinity)
        let estimatedSize = arg.sizeThatFits(size)
    
        arg.constraints.forEach { (constraint) in
            if constraint.firstAttribute == .height {
                constraint.constant = estimatedSize.height
            }
        }
    
    }
    

    这里是视频教程的链接:https://www.youtube.com/watch?v=0Jb29c22xu8

    【讨论】:

    • 我从视频教程中更改了一些代码,以从自定义表格视图单元格 .xib 文件中获取 UIView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2014-06-11
    • 2013-02-07
    相关资源
    最近更新 更多