Reputation: 6051
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.
Upvotes: 2
Views: 300
Reputation: 6051
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 !
Upvotes: -2
Reputation: 1549
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.
Upvotes: 2
Reputation: 1067
//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)
Upvotes: 0
Reputation: 798
I'd suggest you to try this.
[myTextView scrollRangeToVisible:NSMakeRange([myTextView.text length], 0)]
Upvotes: 6