views:

192

answers:

2

Using the code below, I put text from a plist into a textView. The textView is not yet firstresponder; the text is initially for reading only. In iOS4 the goToEndOfNote code positions the cursor at the end of the text AND scrolls to that position. In 3.1.3 it doesn't scroll to the end until the screen is touched (which isn't required unless a change or addition is required), making the textView firstresponder. I would like it to work in 3.1.3 as it does in 4.0. Any ideas please. Thanks.

    ...
    self.temp = [[[NSMutableArray alloc] initWithContentsOfFile:myPlistPath] autorelease]; 
    self.textView.text = [self.temp objectAtIndex:0];
    [self goToEndOfNote];
    //[self performSelector:@selector(goToEndOfNote) withObject:nil afterDelay:0.1];
}

- (void) goToEndOfNote {
    NSUInteger length = self.textView.text.length;  
    self.textView.selectedRange = NSMakeRange(length, 0);
}
A: 

I use setContentOffset:animated to scroll to the top of a UITextView in one of my apps. Should work for scrolling to the bottom, too. Try:

- (void) goToEndOfNote {
    NSUInteger length = self.textView.text.length;  
    self.textView.selectedRange = NSMakeRange(length, 0);
    [textView setContentOffset:CGPointMake(0, length) animated:YES];
}

You could also wrap that up so it only happens for 3.1.3 and below:

- (void) goToEndOfNote {
    NSUInteger length = self.textView.text.length;  
    self.textView.selectedRange = NSMakeRange(length, 0);
    NSString* systemVersion = [[UIDevice currentDevice] systemVersion];
    float version = [systemVersion floatValue];
    if (version < 3.2) {
        [textView setContentOffset:CGPointMake(0, length) animated:YES];
    }
}
Shaggy Frog
Almost but not quite. The textview is most of the screen. In 4.0 the add'l line [textView setContentOffset:CGPointMake(0, length) animated:YES]; causes the last character to scroll to the bottom of textView (good). In 3.1.3, it causes the last character to scroll to about mid-way on the screen (ok) but in both OS versions, if there are just 1 or 2 lines on the screen, they are scrolled off the screen (not so good). I'd prefer scrolling to start only after a screenful is present (as it does in 4.0 with just the 2 original lines in my method).
Matt Winters
A: 

Not sure if this is THE answer but it works.

In 3.1.3, with the original code, the cursor was at the end but the scroll was at the top. In 4.0, both were at the bottom.

NSUInteger length = self.textView.text.length;
self.textView.selectedRange = NSMakeRange(length, 0);

I then noticed that in 3.1.3, switching the 0 and the length, the scroll was at the bottom but the cursor was at the top.

NSUInteger length = self.textView.text.length;
self.textView.selectedRange = NSMakeRange(0, length); 

Putting the two together worked. It scrolls to the bottom NSMakeRange(0, length) presumably to the end of the range, then the NSMakeRange(length, 0) puts the cursor there, all with no change to what it does in 4.0

NSUInteger length = self.textView.text.length;
self.textView.selectedRange = NSMakeRange(0, length); 
self.textView.selectedRange = NSMakeRange(length, 0);
Matt Winters