您的位置:首页 > 产品设计 > UI/UE

iOS UITextView 垂直水平居中

2016-08-04 09:33 375 查看
主要利用的KVO,具体代码:

注册监听

 [self.textView
addObserver:self
forKeyPath:@"contentSize"
options:(NSKeyValueObservingOptionNew)
context:NULL];

 self.textView.text
=self.content;

2.实现

#pragma mark - KVO

-(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};

      

}

//移除

- (void)dealloc{

    

    [self.textView
removeObserver:self
forKeyPath:@"contentSize"
context:NULL];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: