gpt4 book ai didi

iphone - 在 UITextView 中垂直居中文本

转载 作者:IT老高 更新时间:2023-10-28 11:28:00 30 4
gpt4 key购买 nike

我想将文本 垂直 居中在一个大的 UITextView 中,它会填满整个屏幕 - 这样当文本很少时,说几句话,它就是以高度为中心。这不是关于文本居中的问题(可以在 IB 中找到的属性),而是关于将文本 vertically 放在 UITextView if 文本很短,所以UITextView 中没有空白区域。这可以做到吗?提前致谢!

最佳答案

首先在 View 加载时为UITextViewcontentSize键值添加一个观察者:

- (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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com