Created
May 20, 2017 22:52
-
-
Save illescasDaniel/c1a97d0fae8e6cd1ff127bd399671ecd to your computer and use it in GitHub Desktop.
UITextView Center text Vertically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.