Skip to content

Instantly share code, notes, and snippets.

@illescasDaniel
Created May 20, 2017 22:52
Show Gist options
  • Select an option

  • Save illescasDaniel/c1a97d0fae8e6cd1ff127bd399671ecd to your computer and use it in GitHub Desktop.

Select an option

Save illescasDaniel/c1a97d0fae8e6cd1ff127bd399671ecd to your computer and use it in GitHub Desktop.
UITextView Center text Vertically
//
// UITextView+Extension.swift
//
// Created by Daniel Illescas Romero on 21/05/2017.
//
import UIKit
extension UITextView {
/// Modifies the top content inset to center the text vertically.
///
/// Use KVO on the UITextView contentSize and call this method inside observeValue(forKeyPath:of:change:context:)
func alignTextVerticallyInContainer() {
var topCorrect = (self.bounds.size.height - self.contentSize.height * self.zoomScale) / 2
topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect;
self.contentInset.top = topCorrect
}
}
@illescasDaniel
Copy link
Copy Markdown
Author

Did you “use KVO on the UITextView contentSize and call this method inside observeValue(forKeyPath:of:change:context:)”?
@kyzominskiy

@kyzominskiy
Copy link
Copy Markdown

kyzominskiy commented Oct 11, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment