Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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
Ios 在UITextView中垂直居中文本不工作_Ios_Objective C_Uitextview - Fatal编程技术网

Ios 在UITextView中垂直居中文本不工作

Ios 在UITextView中垂直居中文本不工作,ios,objective-c,uitextview,Ios,Objective C,Uitextview,我已经尝试了流行问题()中的许多解决方案 但是,当我尝试添加代码时(我的项目是Obj-C),(来自s.ka的回答): -(void)viewDidLoad{ [textField addObserver:self-forKeyPath:@“contentSize”选项:(NSKeyValueObservingOptionNew)上下文:NULL]; [超级视图下载]; } -(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDi

我已经尝试了流行问题()中的许多解决方案

但是,当我尝试添加代码时(我的项目是Obj-C),(来自s.ka的回答):

-(void)viewDidLoad{
[textField addObserver:self-forKeyPath:@“contentSize”选项:(NSKeyValueObservingOptionNew)上下文: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);
[电视设置内容插图:UIEdgeInsetsMake(topCorrect,0,0,0)];
}
我得到以下错误:“使用未声明的标识符'observeValueforKeyPath'”

我肯定这只是我犯的一个愚蠢的错误,但我无法理解

另一方面:这似乎是将文本iOS 7-9居中的最佳答案。如果它不能在iOS 7-9上运行(我只有iOS 9设备可供测试),请告诉我

我正在运行最新版本的XCode

更新:我能够让错误消失(谢谢,伊曼),但现在它什么也做不了。文本显示,但不居中。如果有帮助,我的文本视图名为quote_text,如果这意味着我应该修改代码。这两种解决方案都不起作用。我不知所措

谢谢

分支机构更新

我认为,因为你没有使用自动布局,无论如何 在viewDidLoad方法中使用此代码

CGFloat topCorrect = ([quote_text bounds].size.height - [quote_text contentSize].height * [quote_text zoomScale])/2.0;
topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
[quote_text setContentInset:UIEdgeInsetsMake(topCorrect,0,0,0)];
CGFloat-topCorrect=([quote\u-text-bounds].size.height-[quote\u-text-contentSize].height*[quote\u-text-zoomScale])/2.0;
topCorrect=(topCorrect<0.0?0.0:topCorrect);
[quote_text setContentInset:UIEdgeInsetsMake(topCorrect,0,0,0)];
[self.yourTextView addObserver:self-forKeyPath:@“contentSize”选项: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<0){
topCorrect=0.0;
}
tv.contentOffset=(CGPoint){.x=0.y=-topCorrect};



我不确定这是如何解决我的主要问题的?@Branch我从你的问题中得到的是,你希望文本在textview中垂直居中。我做了一个更新。但是你能详细说明一下你到底面临什么问题吗?这样可以代替所有的操作代码吗?在我现有的代码中,我在哪里添加你的代码@Iman@Branch将其放置在ViewDiLoad中并删除其余代码。(如果您希望文本在textview中垂直居中)这与OPs问题无关。问题是“使用未声明的标识符”错误。不,不起作用!:(我不知道发生了什么事)。
CGFloat topCorrect = ([quote_text bounds].size.height - [quote_text contentSize].height * [quote_text zoomScale])/2.0;
topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
[quote_text setContentInset:UIEdgeInsetsMake(topCorrect,0,0,0)];
     [self.yourTextView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:NULL];

     - (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;
           if (topCorrect < 0) {
                 topCorrect = 0.0;
         }
     tv.contentOffset = (CGPoint) { .x = 0, .y = -topCorrect };