【发布时间】:2013-10-08 01:49:57
【问题描述】:
以下代码在 iOS
TestController.h
//TODO: Add UITextView in storyboard and tie to textView outlet
#define MAX_TEXT_VIEW_CHARACTERS 1000
@interface TestController : UIViewController {
NSMutableString *_outputText;
NSTimer *_outputTimer;
}
@property (strong, nonatomic) IBOutlet UITextView *textView;
@end
TestController.m
@implementation TestController
@synthesize textView;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_outputText = [NSMutableString stringWithCapacity:MAX_TEXT_VIEW_CHARACTERS];
_outputTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(outputLine:) userInfo:nil repeats:YES];
}
-(void)outputLine:(NSTimer *) theTimer {
static int i = 0;
//Run this 100 times
if (i > 99) {
[_outputTimer invalidate];
return;
}
[self outputToScreen:[NSString stringWithFormat:@"Some string %d\r", ++i]];
}
-(void)outputToScreen:(NSString *)str {
if (!str || !str.length) return; //Nothing to output
NSInteger outputTextSize = _outputText.length;
[_outputText appendString:str];
if (outputTextSize > MAX_TEXT_VIEW_CHARACTERS)
[_outputText deleteCharactersInRange:NSMakeRange(0, outputTextSize - MAX_TEXT_VIEW_CHARACTERS)];
self.textView.text = _outputText;
[self scrollOutputToBottom];
}
-(void)scrollOutputToBottom {
CGPoint p = [textView contentOffset];
[textView setContentOffset:p animated:NO];
[textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
}
@end
【问题讨论】:
-
不管怎样,即使是简单的
setContentOffset电话,我也遇到了问题。内容偏移量发生变化,但视图不滚动。接受的答案有效。 -
好点祖尔。这就是为什么我同时添加了 setContentOffset 和 scrollRageToVisible 以表明这两种滚动方法都不像 iOS 7 中的新 UITextView 那样有效。
-
这仍然是 iOS 10 中的问题吗?
-
这仍然是 iOS 13 中的问题吗? (看来,无论我做什么我都无法让该死的东西滚动。sigh)
标签: ios uiscrollview ios7 uitextview