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];
}
注册监听
[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];
}
相关文章推荐
- css实现水平垂直居中的几种方法
- 绝对定位元素的水平垂直居中
- CSS使DIV层绝对居中【水平和垂直】
- css3中通过设置box-pack和box-align让div里面的各种元素垂直和水平居中的方法
- css 元素水平垂直居中总结
- CSS实现垂直 水平居中
- CSS(3)实现水平垂直居中效果
- CSS垂直水平完全居中手册
- html中div如何水平和垂直居中的几种css方法代码
- 如何实现不定宽高水平和垂直居中
- CSS 各种水平垂直居中
- div,li内部图片自适应水平垂直居中
- div表格定高垂直居中,水平居中
- div+css垂直居中和水平居中
- 简单理解css中的垂直居中和水平居中,即vertical-align和text-align属性
- CSS设置元素水平居中、垂直居中方式汇总
- 垂直水平双向居中的弹窗 以及 移动端屏幕适配
- 不定宽高水平且垂直居中的实现方法
- 【转】如何让DIV水平和垂直居中
- 关于CSS实现水平垂直居中