Graham Carling
Graham Carling

Reputation: 563

What is the best way to display a large amount of formatted text in a single screen in an iOS7 app?

I am making an app and I want to add a terms and conditions page, so I want to have a large scrollable window of text on a single screen. The two classes I've looked at are UIScrollView and UITextView. UIScrollView is easy to scroll with, but I'm not sure how to add all the formatted text that I need without manually creating each label, formatting it, and adding it to the view in the right place. I created a UITextView (non-editable, non-selectable) with attributed text, which gave me what I needed, but then I was unable to get it to scroll past the boundaries of the window (i.e. I couldn't scroll beyond the bottom border of the screen, even after I changed its content size and changed its frame to be much larger than the screen itself). I also tried a merger of the two, with something like this (after disabling scrolling on the text view):

_scrollView.scrollEnabled = YES;
_scrollView.contentSize = CGSizeMake(320, 2000);
[_textView removeFromSuperview];
[_scrollView addSubview:_textView];

But then the text view never appeared in the scroll view. I'm sure this is an issue other people have run into, as many apps have pages similar to the one I'm describing. I'm hoping someone can help me find a way to do this without using a UIScrollView and manually creating a label for each piece of text I want to add.

Upvotes: 2

Views: 2275

Answers (2)

Paulo
Paulo

Reputation: 1245

There is a 3rd way and that is to load a UIWebview with an internal htmlpage / file.

Regarding your UITextview - set the frame size so that it fits within the bounds of the screen/ self.view, if the frame size is larger than screen/ view.frame then you get problems like inability to scroll - this is because as far as the textview is concerned the entire text is already displayed only it is displayed in the part of the frame that is not visible to the view. UITextview behaves like a scrollview with the content size automatically set so all your text should be visible by scrolling.

Regarding UIScrollviews - similar to the discussion re UITextview, you should set the frame size within the bounds of the screen/ self.view. The content size should be large enough to accommodate your biggest object (frame wise). Your idea to place the larger than frame UITextview inside the UIScrollview is sound and should work. simply add textview as a subview of the scrollview.

Upvotes: 0

marcopaivaf
marcopaivaf

Reputation: 309

UITextView inherits from UIScrollView. You don't need to have a UITextView inside a UIScrollView, unless you want to add multiples views into it.

You can create a UITextView that takes the whole screen in a UIViewController like this:

UITextView *myTextView = [[UITextView alloc] initWithFrame:self.view.bounds];
myTextView.text = @"some large text";
myTextView.font = [UIFont fontWithName:@"Arial" size:20];
myTextView.scrollEnabled = YES;
myTextView.editable = NO;
[self.view addSubview:myTextView];

The property contentSize will be set automatically depending on the size of the text. You can also use [myTextView sizeToFit] method to resize the UITextView to fit its text.

Upvotes: 2

Related Questions