0

I'm having a bad time when vertically centering a UITextView text. When I press Enter or when the text doesn't fit in a single line and goes to the next line, the text doesn't stay vertically aligned until I hit another key.

Here are the images: This one is working, working fine

This one is bugged, bug

This one is working again, working again

Here is my code running:

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

I can't seem to find any fix for the new line key which seems to reset the behaviour of the UITextView text until I hit a key.

Any help would be appreciated

Edit: I solved my problem !

Bryan ORTIZ
  • 306
  • 3
  • 17

0 Answers0