我想将文本 垂直 居中在一个大的 UITextView
中,它会填满整个屏幕 - 这样当文本很少时,说几句话,它就是以高度为中心。
这不是关于文本居中的问题(可以在 IB 中找到的属性),而是关于将文本 vertically 放在 UITextView
if中间的问题em> 文本很短,所以UITextView
中没有空白区域。
这可以做到吗?提前致谢!
最佳答案
首先在 View 加载时为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};
}
关于iphone - 在 UITextView 中垂直居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12591192/