Skip to content

Instantly share code, notes, and snippets.

@johanforssell
Last active August 29, 2015 14:07
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 johanforssell/94e089707d5823ab1fdf to your computer and use it in GitHub Desktop.
Save johanforssell/94e089707d5823ab1fdf to your computer and use it in GitHub Desktop.
A useful UITextView subclass
// From https://github.com/Nikita2k/resizableTextView
#import <UIKit/UIKit.h>
// ---------------------------------------
@interface ResizableTextView : UITextView
@end
// ---------------------------------------
@implementation ResizableTextView
- (void) updateConstraints {
// calculate contentSize manually
// ios7 doesn't calculate it before viewDidAppear and we'll get here before
CGSize contentSize = [self sizeThatFits:CGSizeMake(self.frame.size.width, FLT_MAX)];
// set the height constraint to change textView height
[self.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if (constraint.firstAttribute == NSLayoutAttributeHeight) {
constraint.constant = contentSize.height;
*stop = YES;
}
}];
[super updateConstraints];
}
- (void)setContentOffset:(CGPoint)contentOffset
{
// In iOS 8 we seem to be inheriting the content offset from the parent.
// I'm not interested!
// This is the "do nothing" override.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment