ios中具有自动滚动功能的无限Textview
Posted
技术标签:
【中文标题】ios中具有自动滚动功能的无限Textview【英文标题】:Infinite Textview with autoscrolling in ios 【发布时间】:2013-03-08 11:58:55 【问题描述】:实现了 textview 中的自动滚动。但我无法实现无限文本视图。 我想将相同的文本附加到 textview 并无限滚动.....
无限滚动的代码......
-(void)viewWillAppear:(BOOL)animated
if (scrollingTimer == nil)
scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06)
target:self
selector:@selector(autoscrollTimerFired)
userInfo:nil
repeats:YES];
self.textView.contentOffset=CGPointMake(0, -(self.textView.frame.size.height));
- (void) autoscrollTimerFired
CGPoint scrollPoint = self.textView.contentOffset;
scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1);
[self.textView setContentOffset:scrollPoint animated:NO];
【问题讨论】:
【参考方案1】:在达到初始正文高度后,您可以通过滚动回到 UITextView
的开头来模拟这一点。
这是一个“无限”滚动随机 Lorem 段落的示例。
它将段落复制三遍,以确保它看起来像是在永远滚动。
@implementation ViewController
UITextView *infiniteTextView;
NSTimer *scrollTimer;
CGFloat textHeight;
- (void)viewDidLoad
[super viewDidLoad];
self.view.backgroundColor = [UIColor darkGrayColor];
// Create a text view
infiniteTextView = [[UITextView alloc] initWithFrame:CGRectInset(self.view.bounds, 0, self.view.bounds.size.height/4)];
NSString *lorem = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n\n";
infiniteTextView.font = [UIFont systemFontOfSize:15];
CGRect textBounds = [lorem boundingRectWithSize:CGSizeMake(infiniteTextView.bounds.size.width - 16.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@ NSFontAttributeName : infiniteTextView.font context:nil];
textHeight = textBounds.size.height;
//Duplicate the paragraph 3 times
infiniteTextView.text = [lorem stringByAppendingString:[lorem stringByAppendingString:lorem]];
[self.view addSubview:infiniteTextView];
- (void) viewDidAppear:(BOOL)animated
if (scrollTimer == nil)
scrollTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06)
target:self
selector:@selector(autoscrollTimerFired:)
userInfo:nil
repeats:YES];
- (void) autoscrollTimerFired:(id)sender
CGPoint scrollPoint = infiniteTextView.contentOffset;
//Reset the scroll position if we exceed
// the body paragraph height
if( scrollPoint.y > textHeight )
scrollPoint.y = 0;
scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1);
[infiniteTextView setContentOffset:scrollPoint animated:NO];
@end
(在 iPhone Retina 4 英寸目标上试试这个,它可能需要针对 iPad 进行一些调整。)
【讨论】:
以上是关于ios中具有自动滚动功能的无限Textview的主要内容,如果未能解决你的问题,请参考以下文章
iOS tableview上textView在编辑状态时,tableview自动上移的功能