通过将文本值分配放入scrollViewDidScroll方法,它对我有用。
示例片段:
样品.h
...
@interface myRootUIViewController : UIViewController <UIScrollViewDelegate>
...
评论:请记住:不要忘记 UIScrollViewDelegate 协议。
样品.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.
}
评论:
我显然已经用示例命名和值调整了源代码片段。希望没有错别字。但是,代码还包含文本所需帧高度的计算,以防文本的值可以更改,因此实际上需要不同的帧大小。
将实际的文本值分配到 scrollViewDidScroll 方法对我有用,在滚动等期间没有任何闪烁(到目前为止仅在 iPhone 模拟器中测试过)。
希望有帮助。当然,我愿意接受任何建设性的反馈、改进建议或什至其他解决此问题的方法。