更改/动画UITextView中的maximumNumberOfLines?

时间:2019-06-12 16:41:22

标签: ios swift uitextview

我有一个const theData = new Map([ ["x", "ecks"], ["y", "why"] ]); const key = Math.random() < 0.5 ? "x" : "y"; console.log("key = " + key + ", theData.get(key) = " + theData.get(key));,最初我想将其设置为具有UITextView行,但是当用户单击“更多”按钮时,我想将其扩展到全长,我认为这是通过将4移至maximumNumberOfLines或获得较高的数字,例如说0

问题是,在将行从30更改为4(或0)之后,它并未将uitextview中继到其全部高度,因此似乎限制为4行。 / p>

我分别调用30self.setNeedsLayout()来触发布局,但不会恢复到完整高度

我也尝试过在更改行数后没有任何运气的情况下致电self.layoutIfNeeded()

我在这里做什么错了?

谢谢

2 个答案:

答案 0 :(得分:0)

更改textField上的行数只会影响允许textField用来显示其文本的行数。将maximumNumberOfLines设置为0会将文本全部填充到一个“行”上,并在其width的末尾将其截断-这样就不会真正隐藏其余的文本。 / p>

最好不要更改maximumNumberOfLines,而是让文本填充自然的行数,并为UITextView的{​​{1}}设置动画。< / p>

答案 1 :(得分:0)

您可能做错了几件事...

首先,为了“自动调整大小” textView的高度,必须滚动禁用

第二,它不能有固定的高度(高度约束和顶部和底部约束都没有)。


编辑:为澄清起见...当我说“无底限制”时,这并不表示 不能 。相反,不能以防止更改高度的方式设置底部约束。因此,例如,如果textView在表视图单元格中,则底部约束就可以了,只要该单元格的设计和使用方式可以使textView的高度控制(或有助于)单元格的高度


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

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

}

结果:

enter image description here

enter image description here

当然,您需要添加一些代码来处理您的textView包含太多文本的情况,该文本将超出屏幕底部(或超出其Superview的范围)-通过检查结果高度,调整高度和切换滚动,或将textView嵌入UIScrollView中。(例如)。