简体   繁体   English

iOS 7 UITextView垂直对齐

[英]iOS 7 UITextView vertical alignment

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? 我的可编辑UITextView (放在一个UISplitView中作为UITextView委托的简单UIViewController)是不是可能从一开始就显示文本但是在6-7行之后?

截图

I didn't set any particular autolayout or something similar, trying to delete text doesn't help (so no hidden chars or something). 我没有设置任何特定的自动布局或类似的东西,试图删除文本没有帮助(所以没有隐藏的字符或东西)。

I'm using iOS 7 on iPad, in storyboard looks good... The problem is the same on iOS simulator and real devices. 我在iPad上使用iOS 7,故事板看起来不错...... iOS模拟器和真实设备上的问题是一样的。 I'm getting mad :P 我生气了:P

Here's some code. 这是一些代码。 This is the ViewController viewDidLoad() 这是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
}

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的重写函数我正在使用我在StackOverflow上找到的一些代码来模拟视图的占位符(故事板的iPhone版本上的相同内容工作正常)...

// 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;
}

Try to call -sizeToFit after passing the text. 传递文本后尝试调用-sizeToFit This answer could be useful to Vertically align text within a UILabel . 此答案可用于垂直对齐UILabel中的文本
[UPDATE] [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. 问题是,从iOS7,容器视图控制器(如UINavigationController或UITabbarController)可以更改滚动视图(或从其继承的视图)的内容插入,以避免内容重叠。 This happens only if the scrollview is the main view or the first subviews. 仅当scrollview是主视图或第一个子视图时才会发生这种情况。 To avoid that you should disable this behavior by setting automaticallyAdjustsScrollViewInsets to NO, or overriding this method to return NO. 要避免这种情况,您应该通过将automaticallyAdjustsScrollViewInsets设置为NO来禁用此行为,或者重写此方法以返回NO。

I got through the same kind of issue. 我遇到了同样的问题。

Solved it by disabling the automatic scrollView insets adjustement : 通过禁用自动scrollView insets adjustement来解决它:

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){
    self.automaticallyAdjustsScrollViewInsets = NO; // Avoid the top UITextView space, iOS7 (~bug?)
}

This is a fairly common problem, so I would create a simple UITextView subclass, so that you can re-use it and use it in IB. 这是一个相当常见的问题,因此我将创建一个简单的UITextView子类,以便您可以重新使用它并在IB中使用它。

I would used the contentInset instead, making sure to gracefully handle the case where the contentSize is larger than the bounds of the textView 我会使用contentInset,确保优雅地处理contentSize大于textView边界的情况

@interface BSVerticallyCenteredTextView : UITextView

@end

@implementation BSVerticallyCenteredTextView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addObserver:self forKeyPath:@"contentSize" options:  (NSKeyValueObservingOptionNew) context:NULL];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self addObserver:self forKeyPath:@"contentSize" options:  (NSKeyValueObservingOptionNew) context:NULL];
    }
    return self;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentSize"])
    {
        UITextView *tv = object;
        CGFloat deadSpace = ([tv bounds].size.height - [tv contentSize].height);
        CGFloat inset = MAX(0, deadSpace/2.0);
        tv.contentInset = UIEdgeInsetsMake(inset, tv.contentInset.left, inset, tv.contentInset.right);
    }  
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"contentSize"];
}

@end

use - observerForKeyPath with contentSize KeyPath use - 使用带有contentSize KeyPath的observerForKeyPath

Look some code at My Blog (don't focus on Thai Language) 在我的博客上查看一些代码(不要专注于泰语

http://www.macbaszii.com/2012/10/ios-dev-uitextview-vertical-alignment.html http://www.macbaszii.com/2012/10/ios-dev-uitextview-vertical-alignment.html

Inspired by Kiattisak, I've implemented vertical alignment as a category over UITextView so that you can control the vertical alignment of legacy UITextView . 受Kiattisak的启发,我将垂直对齐作为UITextView一个类别实现,这样您就可以控制传统UITextView垂直对齐。

You can find it as a gist here . 你可以在这里找到它作为一个要点

I had the same issue with iOS 8.1, and none of these suggestions worked. 我在iOS 8.1中遇到了同样的问题,但这些建议都没有奏效。

What did work was to go into the Storyboard, and drag my UITableView or UITextView so that it was no longer the first subview of my screen's UIView . 什么的工作是去到故事板,并拖我UITableViewUITextView ,这样它不再是我的屏幕的第一子视图UIView

http://www.codeproject.com/Tips/852308/Bug-in-XCode-Vertical-Gap-Above-UITableView http://www.codeproject.com/Tips/852308/Bug-in-XCode-Vertical-Gap-Above-UITableView

It seems to be linked to having a UIView embedded in a UINavigationController . 它似乎与在UINavigationController嵌入UIView

Bug ? 错误? Bug ? 错误? Did I say "bug" ...? 我说“虫子”了吗?

;-) ;-)

Swift version of Tanguy.G's answer: Swift版本的Tanguy.G的回答:

if(UIDevice.currentDevice().systemVersion >= "7.0") {
            self.automaticallyAdjustsScrollViewInsets = false; // Avoid the top UITextView space, iOS7 (~bug?)
  }

Check top content inset of textView in -viewDidLoad : 检查-viewDidLoad-viewDidLoad顶级内容插入:

NSLog(@"NSStringFromUIEdgeInsets(self.itemTextField.contentInset) = %@", NSStringFromUIEdgeInsets(self.itemTextField.contentInset));

Reset it in storyboard if it is not zero 如果它不是零,则在故事板中重置它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM