Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在iOS 9的UItextView中垂直居中文本_Objective C_Uitextview - Fatal编程技术网

Objective c 在iOS 9的UItextView中垂直居中文本

Objective c 在iOS 9的UItextView中垂直居中文本,objective-c,uitextview,Objective C,Uitextview,由于iOS9更新,下面的代码对我不再有效 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { UITextView *tv = object; CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].h

由于iOS9更新,下面的代码对我不再有效

-(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];

}
-(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文
{
UITextView*tv=对象;
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};
}
-(无效)视图将显示:(BOOL)动画
{
[超级视图将显示:否];
[questionTextView-addObserver:self-forKeyPath:@“contentSize”选项:(NSKeyValueObservingOptionNew)上下文:NULL];
}

iOS 9是否有垂直居中文本的解决方法?

您可以设置contentInset,而不是设置contentOffset(只需更改observeValueForKeyPath方法中的最后一行代码):

-(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文
{
UITextView*tv=对象;
CGFloat topCorrect=([tv bounds].size.height-[tv contentSize].height*[tv zoomScale])/2.0;
topCorrect=(topCorrect<0.0?0.0:topCorrect);
[电视设置内容插图:UIEdgeInsetsMake(topCorrect,0,0,0)];
}
-(无效)视图将显示:(BOOL)动画
{
[超级视图将显示:否];
[questionTextView-addObserver:self-forKeyPath:@“contentSize”选项:(NSKeyValueObservingOptionNew)上下文:NULL];
}
我分别对左、下和右边缘插入使用了0、0和0。确保根据您的用例计算这些

注意:您发布的代码在逻辑上仍然正确。但是,如果您KVO contentOffset,iOS 9会在最后一刻将其设置为0,从而覆盖您对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 setContentInset:UIEdgeInsetsMake(topCorrect,0,0,0)];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:NO];
    [questionTextView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
}