Skip to content

Instantly share code, notes, and snippets.

@kylestew
Created April 18, 2014 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylestew/11028041 to your computer and use it in GitHub Desktop.
Save kylestew/11028041 to your computer and use it in GitHub Desktop.
KSCenteredTextView - Auto Vertical Centering UITextView
#import <UIKit/UIKit.h>
@interface KSCenteredTextView : UITextView
@end
#import "KSCenteredTextView.h"
@implementation KSCenteredTextView
- (void)layoutSubviews {
CGRect saveFrame = self.frame;
[self sizeToFit];
float height = self.frame.size.height;
self.frame = saveFrame;
float inset = (self.frame.size.height - height) / 2.0f;
self.contentOffset = CGPointMake(0, -inset);
}
- (void)setText:(NSString *)text {
[super setText:text];
[self setNeedsLayout];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment