以下代码适用于iOS< 7.0。在iOS 7中,当UITextView正在更新时,滚动将变得不稳定且不稳定。我不确定这是否是iOS 7中的错误,或者我做错了什么。
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
答案 0 :(得分:55)
这适用于iOS7。
-(void) scrollToBottom {
[textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
[textView setScrollEnabled:NO];
[textView setScrollEnabled:YES];
}
答案 1 :(得分:27)
这显然是iOS 7的错误。这是一个解决方法,直到苹果修复它。解决方法基本上是通过从头开始创建UITextView
和NSTextStorage
来实例化NSLayoutManager
。 Apple必须忘记在UITextView
初始化方法中初始化某些内容。我提交了一份错误报告,我希望你也这样做。
// ios7 bug fix
// check if the device is running iOS 7.0 or later
NSString *reqSysVer = @"7.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
if (osVersionSupported) {
NSTextStorage* textStorage = [[NSTextStorage alloc] init];
NSLayoutManager* layoutManager = [NSLayoutManager new];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
[layoutManager addTextContainer:textContainer];
yourTextView = [[UITextView alloc] initWithFrame:someFrameForYourTextView
textContainer:textContainer];
// if using ARC, remove these 3 lines
[textContainer release];
[layoutManager release];
[textStorage release];
}
else {
yourTextView = [[UITextView alloc] initWithFrame:someFrameForYourTextView];
}
答案 2 :(得分:4)
iOS 7中有两个问题可以解释您的问题:
解决方案可能是:
-(void)scrollOutputToBottom {
CGRect caretRect = [textView caretRectForPosition:textView.endOfDocument];
[textView scrollRectToVisible:caretRect animated:NO];
}
答案 3 :(得分:2)
试试这个:
// Don't forget to set textView's delegate
-(void)textViewDidChangeSelection:(UITextView *)textView {
[textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
}
答案 4 :(得分:1)
对于那些使用Swift的人,我在这里发布与RawMean相同的答案(再次感谢!)。 正如我写的那样(2014年12月),问题仍然存在于iOS 8.1中,他的解决方案完美无缺......
var textView: UITextView!
var textStorage: NSTextStorage!
var layoutManager: NSLayoutManager!
var textContainer: NSTextContainer!
override func viewDidLoad() {
textStorage = NSTextStorage()
layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let newTextViewRect = view.bounds
let containerSize = CGSize(width: newTextViewRect.width, height: CGFloat.max)
textContainer = NSTextContainer(size: containerSize)
layoutManager.addTextContainer(textContainer)
textView = UITextView(frame: newTextViewRect, textContainer: textContainer)
textView.delegate = self
view.addSubview(textView)
}
override func viewDidLayoutSubviews() {
textView.frame = view.bounds
}
我使用了scrollRangeToVisible方法在文本添加时在底部平滑滚动...
let length = countElements(textView.text)
let range:NSRange = NSMakeRange(length - 1, 1)
textView.scrollRangeToVisible(range)
答案 5 :(得分:0)
基本上setScrollEnabled = YES需要在调用layoutSubviews之前设置。它对我有用。
答案 6 :(得分:0)
这对我有用。参考:UITextView setText should not jump to top in ios8
self.textView.layoutManager.allowsNonContiguousLayout = NO;
self.textView.text = fileContent;
if(fileContent.length > 1)
{
NSRange range = NSMakeRange(self.textView.text.length - 1, 1);
[self.textView scrollRangeToVisible:range];
}
答案 7 :(得分:0)
Swift 2.0 - IOS 8
这基本上是上面dklt's answer的Swift 2.0版本。以前我使用相同的方法没有scrollEnabled
的2行。大多数时候它工作正常。但是,当几乎在同一时间快速连续调用scrollToBottom()
时,它有时并不起作用。
scrollEnabled
的2行没有多大意义,但添加后,该方法一致!
注意:我已尝试在scrollEnabled
之前或之后将scrollRangeTovisible
的两行放在不同的位置,正如dklt的回答中所述......
只有dklt的原始解决方案适合我。其余的没有。
func scrollToBottom()
{
let range:NSRange = NSMakeRange(self.textView.text.characters.count - 1, 1)
self.textView.scrollRangeToVisible(range)
self.textView.scrollEnabled = false
self.textView.scrollEnabled = true
}
答案 8 :(得分:-1)
请尝试此解决方案
-(void) scrollToBottom {
[textView setContentOffset:CGPointMake(0.0, textView.contentSize.height) animated:YES];
}