它對我有用,方法是將文本值分配放入scrollViewDidScroll方法中。
的示例代碼段:
SAMPLE.h
...
@interface myRootUIViewController : UIViewController <UIScrollViewDelegate>
...
評論: 只是要記住:不要忘記UIScrollViewDelegate協議。
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.
}
評論:
我已經調整了源代碼段與樣品namings和價值明顯。希望沒有錯別字。但是,該代碼還包含文本所需幀高的計算,以防文本值發生變化,因此實際需要不同的幀大小。
將實際的文本值賦值放到scrollViewDidScroll方法中,對於我來說,在滾動等過程中沒有任何類型的閃爍(到目前爲止只在iPhone模擬器中測試過)。
希望有所幫助。當然,我對任何建設性的反饋意見,改進建議甚至解決這個問題的其他方法都是開放的。
感謝您的回答,我也認爲這個bug與UITextView是UIScrollView的子類的事實有關,因爲這也發生在UIWebView。但是,我不認爲這樣做是對付系統,因爲它是顯示一組步驟或指令的自然方式 – muesan 2008-10-02 09:46:30
我同意但我不認爲這是一個錯誤,這是一個功能。我沒有看到蘋果公司可以避免這種情況。 – 2009-04-15 11:10:29