这是一个有点棘手的修复。您需要观察 UITextView
的 contentSize
属性,然后像这样调整其 contentSize
和 contentOffset
:
- (void) viewDidLoad {
[self.textView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
[super viewDidLoad];
}
- (void)observeValueForKeyPath:(NSString *)iKeyPath ofObject:(id)iObject change:(NSDictionary *)iChange context:(void *)iContext {
UITextView *myTextView = (UITextView *)iObject;
CGFloat topCorrect = (myTextView.bounds.size.height - [myTextView contentSize].height * [myTextView zoomScale]) / 2.0;
topCorrect = (topCorrect < 0.0 ? 0.0 : topCorrect);
myTextView.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}