输入换行符时UITextView垂直文本对齐方式

时间:2016-11-07 14:29:58

标签: ios swift uitextview uitextviewdelegate

我在垂直居中UITextView文字时遇到了不好的时间。当我按Enter或当文本不适合单行并转到下一行时,文本不会保持垂直对齐,直到我按下另一个键。

以下是图片: 这个工作正常,working fine

这个错误,bug

这个再次运作,working again

这是我的代码运行:

func textViewDidChange(textView: UITextView) {
        messageTextViewHeightConstraint.constant = messageTextView.contentSize.height
        sendBarHeightConstraint.constant = messageTextView.contentSize.height + 11
        secondBarHeightConstraint.constant = messageTextView.contentSize.height
        alignTextVerticalInTextView(messageTextField)
        self.view.layoutIfNeeded()
}

func alignTextVerticalInTextView(textView :UITextView) {
    let size = textView.sizeThatFits(CGSizeMake(CGRectGetWidth(textView.bounds), CGFloat(MAXFLOAT)))
    var topoffset = (textView.bounds.size.height - size.height * textView.zoomScale) / 2.0
    topoffset = topoffset < 0.0 ? 0.0 : topoffset
    textView.contentOffset = CGPointMake(0, -topoffset)
}

我似乎无法找到新线路键的任何修复,这似乎会重置UITextView文字的行为,直到我按下一个键。

任何帮助将不胜感激

编辑:我解决了我的问题!

0 个答案:

没有答案
相关问题