【问题标题】:UITextView content changing position while increasing heightUITextView 内容在增加高度时改变位置
【发布时间】:2014-10-29 07:07:11
【问题描述】:

我正在尝试使用textViewDidChange 委托调用根据其内容的大小更改UITextView 的高度。但是在输入第一行时文本会向上推一点,并在输入下一行时更正到旧位置,对于添加的每一行交替重复。

func textViewDidChange(textView: UITextView!) {
    var computedHeightDifference = textView.contentSize.height - textView.frame.size.height
    if(computedHeightDifference != 0){
        textView.frame.size.height = textView.contentSize.height
    }

}

我尝试使用textView.sizeToFit() 而不是完整块,但添加每一行时文本视图会闪烁(在电话应用程序中添加新联系人时,可以在备注字段中注意到相同的行为。

我已将完整代码上传至GitHub

【问题讨论】:

    标签: ios swift uitextview uitextviewdelegate ios8.1


    【解决方案1】:

    您设置的高度不够大。您需要考虑文本容器的插图。

    这样做:

    func textViewDidChange(textView: UITextView!) {
        var computedHeightDifference = textView.contentSize.height - (textView.frame.size.height + textView.textContainerInset.top + textView.textContainerInset.bottom)
        if(computedHeightDifference != 0){
            textView.frame.size.height = textView.contentSize.height + textView.textContainerInset.top + textView.textContainerInset.bottom
        }
    
    }
    

    【讨论】:

    • 这有帮助。谢谢!我只看ContentInset 没注意到TextContainerInset
    猜你喜欢
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 2019-11-08
    • 2013-03-29
    • 2018-08-16
    • 2011-11-26
    相关资源
    最近更新 更多