iOS 7 UITextView垂直对齐
我的可编辑 UITextView
怎么可能(放在作为委托的 UISplitView
内的简单UIViewController中)对于 UITextView
)是不是从一开始就显示文字但是在6-7行之后?
How is that possible that my editable UITextView
(placed inside a straightforward UIViewController inside a UISplitView
that acts as delegate for the UITextView
) is not showing text from the beginning but after something like 6-7 lines?
我没有设置任何特定的自动布局或其他东西类似的,尝试删除文本没有帮助(所以没有隐藏的字符或其他东西)。
I didn't set any particular autolayout or something similar, trying to delete text doesn't help (so no hidden chars or something).
我在iPad上使用iOS 7,在故事板中看起来不错......
iOS模拟器和真实设备上的问题是一样的。我生气了:P
I'm using iOS 7 on iPad, in storyboard looks good... The problem is the same on iOS simulator and real devices. I'm getting mad :P
这是一些代码。这是ViewController viewDidLoad()
Here's some code. This is the ViewController viewDidLoad()
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.itemTextField.delegate = self;
self.itemTextField.text = NSLocalizedString(@"NEWITEMPLACEHOLDER", nil);
self.itemTextField.textColor = [UIColor lightGrayColor]; //optional
}
以下是 UITextView
我正在使用我在StackOverflow上找到的一些代码来模拟视图的占位符(故事板的iPhone版本上的相同内容工作正常)...
And here are the overridden functions for the UITextView
I'm using some code I've found on StackOverflow to simulate a placeholder for the view (the same stuff on iPhone version of the storyboard works fine)...
// UITextView placeholder
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:NSLocalizedString(@"NEWITEMPLACEHOLDER", nil)]) {
textView.text = @"";
textView.textColor = [UIColor blackColor]; //optional
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:@""]) {
textView.text = NSLocalizedString(@"NEWITEMPLACEHOLDER", nil);
textView.textColor = [UIColor lightGrayColor]; //optional
}
[textView resignFirstResponder];
}
-(void)textViewDidChange:(UITextView *)textView
{
int len = textView.text.length;
charCount.text = [NSString stringWithFormat:@"%@: %i", NSLocalizedString(@"CHARCOUNT", nil),len];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return YES;
}
尝试拨打 -sizeToFit 。此答案可能对垂直对齐UILabel中的文本很有用。
[更新]
我更新此答案o使其更具可读性。
问题是,从iOS7开始,容器视图控制器(如UINavigationController或UITabbarController)可以更改滚动视图(或从其继承的视图)的内容插入,以避免内容重叠。仅当scrollview是主视图或第一个子视图时才会发生这种情况。要避免这种情况,您应该通过将 automaticAdjustsScrollViewInsets
设置为NO来禁用此行为,或者重写此方法以返回NO。
Try to call -sizeToFit
after passing the text. This answer could be useful to Vertically align text within a UILabel.
[UPDATE]
I update this answer o make it more readable.
The issue is that from iOS7, container view controllers such as UINavigationController or UITabbarController can change the content insets of scroll views (or views that inherit from it), to avoid content overlapping. This happens only if the scrollview is the main view or the first subviews. To avoid that you should disable this behavior by setting automaticallyAdjustsScrollViewInsets
to NO, or overriding this method to return NO.