Hi,
it works for me by placing the text value assignment into the scrollViewDidScroll method.
Sample snippets:
SAMPLE.h
...
@interface myRootUIViewController : UIViewController <UIScrollViewDelegate>
...
Comment:
Just to remember: don't forget the UIScrollViewDelegate protocol.
SAMPLE.m
- (void)viewDidLoad {
... whatever is created before and/or after...
NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nunc semper lacus quis erat. Cras sapien magna, porta non,
suscipit nec, egestas in, arcu. Maecenas sit amet est.
Quisque felis risus, tempor eu, dictum ac, volutpat id,
libero. Ut gravida, purus vitae interdum elementum, tortor
justo porttitor nisi, id rhoncus massa.";
// calculate the required frame height according to defined font size and
// given text
CGRect frame = CGRectMake(0.0, 500.0, self.view.bounds.size.width, 1000.0);
CGSize calcSize = [text sizeWithFont:[UIFont systemFontOfSize:13.0]
constrainedToSize:frame.size lineBreakMode: UILineBreakModeWordWrap];
// for whatever reasons, contraintedToSize seem only be able to
// calculate an appropriate height if the input frame height is larger
// than required. Means: if your text requires height=250 and input
// frame height=100, then this method won't give you the expected
// result.
frame.size = calcSize;
frame.size.height += 0; // calcSize might be not pixel-precise,
// so add here additional padding pixels
UITextView * tmpTextView = [[UITextView alloc]initWithFrame:frame];
// do whatever adjustments
tmpTextView.backgroundColor = [UIColor blueColor]; // show area explicitly (dev
// purpose)
self.myTextView = tmpTextView;
self.myTextView.editable = NO;
self.myTextView.scrollEnabled = NO;
self.myTextView.multipleTouchEnabled = NO;
self.myTextView.userInteractionEnabled = NO; // pass on events to parentview
self.myTextView.font = [UIFont systemFontOfSize:13.0];
[tmpTextView release];
[self.scrollView addSubview:self.myTextView];
}
...
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// for simplicity text is repeated again, of course it can be a member var/etc...
NSString * text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nunc semper lacus quis erat. Cras sapien magna, porta non,
suscipit nec, egestas in, arcu. Maecenas sit amet est.
Quisque felis risus, tempor eu, dictum ac, volutpat id,
libero. Ut gravida, purus vitae interdum elementum, tortor
justo porttitor nisi, id rhoncus massa.";
self.myTextView.text = text; // assign value within this method and it is
// painted as expected.
}
Comment:
I have adjusted the source code snippet with sample namings and values obviously. Hopefully there is no typo. However, the code contains also the calculation of the required frame height for the text, in case the text's value can change and therefore would require different frame sizes actually.
Placing the actual text value assignment into the scrollViewDidScroll method worked for me without any kind of flashing during scrolling etc. (so far only tested in iPhone Simulator).
Hope that helps. Of course I am open for any constructive feedback, improvement proposals or even other ways to solve this isuse.