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

  

posted @ 2019-08-02 15:29  willbin  阅读(250)  评论(0编辑  收藏  举报