2
votes

I would like to create an app that has a function like iOS 7's Notes application. Basically there is one row on the top that has date and time.

My solution is to put the UITextView inside UITableView. First row is UILabel with date and time, the second row is UITextView.

I change both UITextView and UITableViewCell height according to UITextView ContentSize.

The problem is the UITextView size is large so it doesn't automatically scroll when the user hit return key.

Is there any solution to make it scroll as normal?

2

2 Answers

1
votes

UITextView is a subclass of UIScrollView. I will suggest an alternative method of implementing a similar functionality. Add the label view as a subview of the text view, and set a contentInset top value of the height of the label.

UILabel* label = [UILabel new];
label.text = @"Test";
[label sizeToFit];

CGRect frame = label.frame;
frame.origin.y -= frame.size.height;
[label setFrame:frame];

[self.textView addSubview:label];

[self.textView setContentInset:UIEdgeInsetsMake(label.frame.size.height, 0, 0, 0)];

Sample project: http://sdrv.ms/16JUlVD

0
votes

Try this solution. Fix is based on inheritance. But logic can be used at any place after UITextView text was changed. I have taken some useful code blocks from here:

http://craigipedia.blogspot.ru/2013/09/last-lines-of-uitextview-may-scroll.html

and edited by me for my solution. Should work.

@interface CustomTextView : UITextView

@end

@implementation CustomTextView

-(id)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

-(void)textDidChange:(NSNotification *)notification {

    //iOS 7 UITextView auto scroll fix.
    NSRange caretRange = self.selectedRange;
    if (caretRange.location == self.text.length) {
        CGRect textRect = [self.layoutManager usedRectForTextContainer:self.textContainer];
        CGFloat sizeAdjustment = self.font.lineHeight * [UIScreen mainScreen].scale;

        if (textRect.size.height >= self.frame.size.height - sizeAdjustment) {
            if ([[self.text substringFromIndex:self.text.length - 1] isEqualToString:@"\n"]) {
                [UIView animateWithDuration:0.2 animations:^{
                    [self setContentOffset:CGPointMake(self.contentOffset.x, self.contentOffset.y + sizeAdjustment)];
                }];
            }
        }
    }
    //end of fix
}

@end