2013-03-08 52 views
3

使用textview中的自动滚动功能。但我无法实现无限的textview。 我想追加同一文本TextView的和无限滚动.....在ios中使用自动滚动的无限文本视图

对于无限滚动的代码.......

-(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视网膜4英寸的目标,可能需要一些调整为iPad。)

相关问题