Vertical Center TextView . 竖直居中的UITextView

@interface VerticalCenterTextView : UITextView
@end

@implementation VerticalCenterTextView

- (void) layoutSubviews;
{
    [super layoutSubviews];
    
    CGSize contentSize = [self sizeThatFits:CGSizeMake(self.bounds.size.width, CGFLOAT_MAX)];
    
    if (contentSize.height < self.bounds.size.height) {
        CGFloat topCorrect = (self.bounds.size.height - contentSize.height * self.zoomScale) / 2.0;
        topCorrect = (topCorrect < 0.0 ? 0.0 : topCorrect);
        self.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
    }   
}

@end

  

原文地址:https://www.cnblogs.com/willbin/p/11288816.html