Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITextView中更改最大行数/设置其动画?_Ios_Swift_Uitextview - Fatal编程技术网

Ios 在UITextView中更改最大行数/设置其动画?

Ios 在UITextView中更改最大行数/设置其动画?,ios,swift,uitextview,Ios,Swift,Uitextview,我有一个UITextView,最初我想将其设置为4行,但是当用户单击“阅读更多”按钮时,我想将其扩展到全长,我假设这是通过将maximumnumnumberoflines设置为0或设置为一个较高的数字,例如30 问题是,在将行从4更改为0(或30)后,它不会将uitextview重新显示为其全高,似乎限制在4行 我调用self.setNeedsLayout()和self.layoutifneed()来触发布局,但它不会恢复到其全高 我还尝试在更改行数后调用了descriptionTextView

我有一个
UITextView
,最初我想将其设置为
4行,但是当用户单击“阅读更多”按钮时,我想将其扩展到全长,我假设这是通过将
maximumnumnumberoflines
设置为
0
或设置为一个较高的数字,例如
30

问题是,在将行从
4
更改为
0
(或
30
)后,它不会将uitextview重新显示为其全高,似乎限制在4行

我调用
self.setNeedsLayout()
self.layoutifneed()
来触发布局,但它不会恢复到其全高


我还尝试在更改行数后调用了
descriptionTextView.invalidateIntrinsicContentSize()
,但没有成功

我做错了什么


谢谢

更改
文本字段
上的行数只会影响
文本字段
用于显示文本的行数。将
maximumNumberOfLines
设置为
0
会将文本全部填充到一个“行”上,并在
宽度的末尾将其截断,这样就不会真正隐藏剩余的文本


与其更改
maximumNumberOfLines
,不如让文本填充自然的行数,并设置
UITextView
heightAnchor
动画。

您可能做错了几件事

首先,为了“自动调整”文本视图的高度,必须禁用滚动

其次,它不能有固定的高度(既不是高度约束,也不是顶部和底部约束)


编辑:澄清。。。当我说“无底部约束”时,这并不意味着它不能有底部约束。相反,不能以阻止textView高度更改的方式设置底部约束。因此,例如,如果textView位于表视图单元格中,则底部约束是可以的,只要单元格的设计和使用方式是textView的高度控制(或有助于)单元格的高度


这是一个简单的示例,将在4行和零行之间切换textView(显示所有文本内容):

结果:


当然,您需要添加一些代码来处理这样一种情况:您的textView包含的文本太多,以至于超出了屏幕底部(或超出了其superview的边界)——通过检查结果高度、调整高度并切换滚动,或者将textView嵌入
UIScrollView
(例如).

我尝试了height方法,结果是一样的,将其设置为0并没有将其填充到一行,它只是没有任何操作,因此我认为这里有一个更大的问题我觉得我应该指定此uitextview位于表格单元格中,它需要一个底部/高度约束来缩放单元格高度,在这种情况下,当扩展内容时,内容将始终超出单元格底部。我不希望它滚动,所以单元格高度需要调整adjust@jwarris91-这将在表格视图单元格中的底部约束下正常工作。。。然后,textView的高度将定义单元格的高度。我编辑了我的答案以反映这一点。invalidateIntrinsicContentSize为我做了这件事!谢谢!:)
class ExpandingTextViewViewController: UIViewController {

    let descriptionTextView: UITextView = {
        let v = UITextView()
        v.translatesAutoresizingMaskIntoConstraints = false
        // disable scrolling
        v.isScrollEnabled = false
        // give it a background color to make it easy to see the frame
        v.backgroundColor = .yellow
        return v
    }()

    let theButton: UIButton = {
        let v = UIButton()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .red
        v.setTitle("Toggle TextView", for: .normal)
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(theButton)
        view.addSubview(descriptionTextView)

        NSLayoutConstraint.activate([

            // button 40-pts from the top, centered horizontally
            theButton.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 40.0),
            theButton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0.0),

            // textView 40-pts from bottom of button, 20-pts padding left and right
            //  NO height or bottom constraint
            descriptionTextView.topAnchor.constraint(equalTo: theButton.bottomAnchor, constant: 40.0),
            descriptionTextView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20.0),
            descriptionTextView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20.0),

            ])

        // give the textView some sample text
        descriptionTextView.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

        // start with max number of lines set to 4
        descriptionTextView.textContainer.maximumNumberOfLines = 4

        theButton.addTarget(self, action: #selector(toggleTextView), for: .touchUpInside)

    }

    @objc func toggleTextView() -> Void {
        // toggle max number of lines between 4 and Zero
        descriptionTextView.textContainer.maximumNumberOfLines =
            (descriptionTextView.textContainer.maximumNumberOfLines == 4) ? 0 : 4
        // tell auto-layout abour the change
        descriptionTextView.invalidateIntrinsicContentSize()
    }

}