我尝试过许多常见问题的解决方案(Center text vertically in a UITextView)
但是,当我尝试添加代码(我的项目是Obj)时,(来自s.ka的答案):
- (void) viewDidLoad {
[textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
[super viewDidLoad];
}
-(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)];
}我得到以下错误:“使用未声明的标识符'observeValueforKeyPath‘”
我肯定这只是我犯的一个愚蠢的错误,但我搞不懂。
从侧面看,这似乎是对文本iOS 7-9进行居中的最佳答案.如果它不能在iOS 7-9上工作(我只有iOS 9设备可以测试),请告诉我。
我正在运行最新版本的XCode。
更新:我能够让错误消失(谢谢,伊曼),但是现在它什么也没做。文字显示,但不是居中的。如果有帮助,我的文本视图名为quote_text,如果这意味着我应该修改代码。两种解决方案都不起作用。我不知所措。
谢谢!
分支
发布于 2016-04-09 14:31:25
更新
我认为因为您没有使用Autolayout,所以在您的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)];发布于 2016-04-09 17:16:47
[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 };https://stackoverflow.com/questions/36517932
复制相似问题