iOS 7 UITextView 垂直对齐 [英] iOS 7 UITextView vertical alignment

查看:21
本文介绍了iOS 7 UITextView 垂直对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的可编辑 UITextView(放置在作为 UITextView 的委托的 UISplitView 内的一个简单的 UIViewController 中)怎么可能是不是从一开始就显示文本而是在 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 的重写函数)...

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 等容器视图控制器可以更改滚动视图(或从它继承的视图)的内容插入,以避免内容重叠.仅当滚动视图是主视图或第一个子视图时才会发生这种情况.为避免这种情况,您应该通过将 automaticallyAdjustsScrollViewInsets 设置为 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.

这篇关于iOS 7 UITextView 垂直对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆