Monday, April 11, 2016

Scrolling to the end of UITextView's text

Leave a Comment

I have some contents in a view (such as images, labels) and the last item is a description (UITextView). Now I am trying to scroll the contents dynamically based on UITextView text. Here is my code:

- (void)viewDidAppear:(BOOL)animated {      _descriptions.scrollEnabled = NO;     [_descriptions sizeToFit];      _infoScrollView.contentSize = CGSizeMake(_contentsOnInfoView.frame.size.width,                                              _contentsOnInfoView.frame.size.height + _descriptions.frame.size.height); } 

Here is the result:

As you can see, there is lots of empty space. I need to scroll to the end of text.

4 Answers

Answers 1

I'd suggest you to try this.

[myTextView scrollRangeToVisible:NSMakeRange([myTextView.text length], 0)] 

Answers 2

You should write you code in

-(void) viewDidLayoutSubviews {} 

In this method,All components have frame according to running device size. and execute before appear the view.

Answers 3

//may be it will work for you NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];     //set the line break mode     paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;      NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys: description.font,                               NSFontAttributeName,                               paragraphStyle,                               NSParagraphStyleAttributeName,                               nil];       CGRect rect = [description.text boundingRectWithSize:CGSizeMake(description.size.width, FLT_MAX)                                                   options:NSStringDrawingUsesLineFragmentOrigin                                                attributes:attrDict                                                   context:nil];     CGSize size = rect.size;    _infoScrollView.contentSize = CGSizeMake(_contentsOnInfoView.frame.size.width,description.frame.origin.y + size.height + 20) 

Answers 4

I just found the answer,in the UI there is line that separates descriptions with the some infos ! I just calculate the line's y and sum up with the descriptions height :

_descriptions.scrollEnabled = NO; [_descriptions sizeToFit]; _infoScrollView.contentSize = CGSizeMake(_infoView.frame.size.width, _line.frame.origin.y + _descriptions.bounds.size.height); 

now it works fine !

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment