我的应用程序设计中有一个问题。我希望将UITextView的垂直对齐设置为中间。请告诉我如何从界面构建器中设置UITextView的垂直对齐。
我的应用程序设计中有一个问题。我希望将UITextView的垂直对齐设置为中间。请告诉我如何从界面构建器中设置UITextView的垂直对齐。
通过观察contentSize属性的变化并设置contentOffset,使其正常工作。
- (void)viewDidLoad{
[super viewDidLoad];
[myTextView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *myView = object;
CGFloat offSet = ([myView bounds].size.height - [myView contentSize].height * [myView zoomScale])/2.0;
offSet = offSet < 0.0 ? 0.0 : offSet;
[myView setContentOffset:CGPointMake(0, -offSet) animated:NO];
}
[tv removeObserver:self forKeyPath:@"contentSize"];
- Vaibhav Saran*yourtextviewname*.textAlignment=UITextAlignmentCenter;