Skip to content

Instantly share code, notes, and snippets.

@xandrucea
Last active December 21, 2015 22:39
Show Gist options
  • Save xandrucea/6376993 to your computer and use it in GitHub Desktop.
Save xandrucea/6376993 to your computer and use it in GitHub Desktop.
!!!!!!
On every user input the methods "textViewDidChange", "resizeTextViewHeight" and "textView:shouldChangeTextInRange" will be called
!!!!!!
hide keyboard when pressing return button
-----------------------------------------
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
method that will be called for each keyboard press
--------------------------------------------------
- (void)textViewDidChange:(UITextView *)textView;
center text vertically inside a textview
----------------------------------------
[self.textViewDescription addObserver:self
forKeyPath:@"contentSize"
options:(NSKeyValueObservingOptionNew)
context:NULL];
[super viewDidLoad];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *tv = object;
CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment