Fra*_*sco 3 objective-c uitextview
自iOS 9更新以来,以下代码对我来说不再适用.
-(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) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:NO];
[questionTextView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
}
Run Code Online (Sandbox Code Playgroud)
对于iOS 9,是否有垂直居中文本的解决方法?
您可以设置contentInset而不是设置contentOffset(只需更改observeValueForKeyPath方法中的最后一行代码):
-(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 setContentInset:UIEdgeInsetsMake(topCorrect,0,0,0)];
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:NO];
[questionTextView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
}
Run Code Online (Sandbox Code Playgroud)
我分别使用0,0和0作为左,右和右边缘插图.确保为您的用例计算这些.
注意:您发布的代码在逻辑上仍然正确.但是,如果您对contentOffset进行了KVO,则iOS 9会在最后一刻将其设置为0,从而覆盖对contentOffset的更改.
| 归档时间: |
|
| 查看次数: |
1761 次 |
| 最近记录: |