scroll the text inside a UIScrollView

2019-08-31 00:48发布

问题:

I have a controller view, that includes, (image, and text).

the two components are in UIScrollView, the image is UIImageView, and the text is UITextView (scroll is enabled). while i am trying to scroll the whole screen, the text doesn't scroll with it, because it has its own scroll.

what I need is to scroll the text with the whole screen scroller. (Merge the scroll of the text with the scroll of the whole one).

should I change the type of text to label instead of textview or what should I do exactly?

Thank You,

回答1:

use this method :

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if(textView==your_textview)
    {
        [yourscroolview setContentOffset:CGPointMake(0,textView.center.y-180) animated:YES];
    }


}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    [yourscroolview resignFirstResponder];
    [yourscroolview setContentOffset:CGPointMake(0,0) animated:YES];

}


回答2:

just add this code in you viewWillAppear: method if text to UITextView are assign at Load time..

yourTextView.frame = CGRectMake(yourTextView.frame.origin.x, yourTextView.frame.origin.y, yourTextView.frame.size.width, yourTextView.contentSize.height);
yourTextView.text=@"Your Text";
float fscrview = yourTextView.frame.origin.y + yourTextView.frame.size.height + 20;
yourScrollView.contentSize=CGSizeMake(320, fscrview);

For Ex.

yourTextView.delegate = self;
yourTextView.userInteractionEnabled = NO;
yourTextView.backgroundColor = [UIColor clearColor];
yourTextView.font = [UIFont fontWithName:@"Helvetica" size:12.0];
yourTextView.textColor = [UIColor whiteColor];
yourTextView.text = @"Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.<br />\r\nLorem ipsum dolor sit amet, consetetur sadipscing elitr.<br />\r\nLorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr. Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.<br />\r\nLorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr. Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.Lorem ipsum dolor sit amet, consetetur sadipscing elitr.";
yourTextView.frame = CGRectMake(yourTextView.frame.origin.x, yourTextView.frame.origin.y, yourTextView.frame.size.width, yourTextView.contentSize.height);
float fscrview = yourTextView.frame.origin.y + yourTextView.frame.size.height + 20;
yourScrollView.contentSize=CGSizeMake(320, fscrview);