Best way to add a large piece of text to a UIScrollView?

What is the best way to display a large piece of text (taken from a .txt file in an application) in a UIScrollView so that the user can scroll it? The length of the text is variable.

+4
source share
4 answers

Put the UITextView in the UIScrollView.

-1
source

In the Builder interface, open the attribute inspector (if it is not already open - command-1) and uncheck the "Editable" box.

Also note the scroll section below. Make sure the "Scroll" checkbox is selected.

Hope this helps someone (post is a year, so I think that the one who posted it does not need this information).

+4
source

One of the ways I worked for me is to create a UILabel, set the text, and then set the scrollview content size to fit it. Here is an example

Quote:

// alocate and initialize scroll UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)]; // alocate and initialize label UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)]; // add long text to label myLabel.text = @"Lorem ipsum... long text here"; // set line break mode to word wrap myLabel.lineBreakMode = UILineBreakModeWordWrap; // set number of lines to zero myLabel.numberOfLines = 0; // resize label [myLabel sizeToFit]; // set scroll view size myScroll.contentSize = CGSizeMake(myScroll.contentSize.width, myLabel.frame.size.height); // add myLabel [myScroll addSubview:myLabel]; // add scroll view to main view [self.view addSubview:myScroll]; 
0
source

Using UITextView in UIScrollView. I cannot recommend this because UITextView is a subclass of UIScrollView. Apple also recommends the same.

Use UILabel in this case as a subview,

-1
source

Source: https://habr.com/ru/post/1304688/


All Articles