内容简介:翻译自:https://stackoverflow.com/questions/17024375/uitextview-aligning-text-vertically-center
我想在UItextView中垂直居中对齐文本.
我正在使用以下代码
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}
;
不知怎的,这在iOS 5中不起作用,因为返回的contentSize与我在iOS6中得到的不同.
任何想法为什么相同textView的contentSize在iOS 5和iOS 6中有所不同?
加载视图时,为UITextView的contentSize键值添加一个观察者: –
- (void) viewDidLoad {
[textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
[super viewDidLoad];
}
每次contentSize值更改时调整contentOffset: –
-(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};
}
希望它能帮到你……
你可以从中获取指南
翻译自:https://stackoverflow.com/questions/17024375/uitextview-aligning-text-vertically-center
以上所述就是小编给大家介绍的《ios – UITextView垂直居中对齐文本》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
JS 压缩/解压工具
在线压缩/解压 JS 代码
CSS 压缩/解压工具
在线压缩/解压 CSS 代码