Guest User

Untitled

a guest
Mar 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. UITextView *tv = object;
  2. CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
  3. topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
  4. tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect}
  5.  
  6. - (void) viewDidLoad {
  7. [textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
  8. [super viewDidLoad];
  9. }
  10.  
  11. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  12. UITextView *tv = object;
  13. CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
  14. topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
  15. tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
  16. }
  17.  
  18. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  19. {
  20. UITextView *tv = object;
  21.  
  22. CGFloat height = [tv bounds].size.height;
  23. CGFloat contentheight;
  24.  
  25. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
  26. contentheight = [tv sizeThatFits:CGSizeMake(tv.frame.size.width, FLT_MAX)].height;
  27. NSLog(@"iOS7; %f %f", height, contentheight);
  28. }else{
  29. contentheight = [tv contentSize].height;
  30. NSLog(@"iOS6; %f %f", height, contentheight);
  31. }
  32.  
  33. CGFloat topCorrect = height - contentheight;
  34. topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
  35. tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
  36. }
  37.  
  38. #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
  39.  
  40. static NSString *const kContentOffsetKeyPath = @"contentOffset";
  41.  
  42. -(void)viewDidLoad {
  43. [self.replyTextView addObserver:self
  44. forKeyPath:kContentOffsetKeyPath
  45. options:(NSKeyValueObservingOptionNew)
  46. context:NULL];
  47. }
  48.  
  49. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  50. if ([keyPath isEqualToString:kContentOffsetKeyPath]) {
  51. // To keep scrolling to bottom while typing and text content view is larger than maximum limit
  52. if (textView.contentSize.height > singleLineHeight) {
  53. UITextView *textView = object;
  54. CGFloat topCorrect = ([textView bounds].size.height - [textView contentSize].height * [textView zoomScale]) / 2.0;
  55. CGFloat fineTune = -3.0;
  56. topCorrect = (topCorrect < fineTune ? fineTune : topCorrect);
  57. // To avoid recursion
  58. [textView removeObserver:self forKeyPath:kContentOffsetKeyPath];
  59. textView.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
  60. // add observer back
  61. [textView addObserver:self
  62. forKeyPath:kContentOffsetKeyPath
  63. options:(NSKeyValueObservingOptionNew)
  64. context:NULL];
  65. }
  66. }
  67. }
  68.  
  69. - (void)dealloc {
  70. [self.replyTextView removeObserver:self forKeyPath:kContentOffsetKeyPath];
  71. }
Add Comment
Please, Sign In to add comment