简体   繁体   English

将大块文本添加到UIScrollView的最佳方法是什么?

[英]Best way to add a large chunk of text to a UIScrollView?

What is the best way to display a large chunk of text (taken from a .txt file in the app) in a UIScrollView so a user can scroll about it? 在UIScrollView中显示大块文本(从应用程序中的.txt文件中获取)的最佳方法是什么,以便用户可以滚动它? Length of the text is variable. 文本的长度是可变的。

On Interface Builder open the Attributes Inspector (if not already open - command-1) and uncheck "Editable". 在Interface Builder上打开Attributes Inspector(如果尚未打开 - command-1)并取消选中“Editable”。

Also notice there's a Scroll View section below. 另请注意下面有一个Scroll View部分。 Make sure "Scrolling" is checked. 确保选中“滚动”。

Hope this helps somebody (the post is a year old so I guess by now the one who posted it doesn't need this info). 希望这对某人有帮助(帖子已经有一年了,所以我想现在发布它的人不需要这个信息)。

One way I had working for me is to create UILabel, set text and then set content size of scrollview by it size. 我为我工作的一种方法是创建UILabel,设置文本,然后按大小设置scrollview的内容大小。 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];

I came here looking for an answer and found that all answers are bad - or flat out wrong. 我来到这里寻找答案,发现所有答案都很糟糕 - 或者说错了。

The proper way to do this is using UITextView by itself. 正确的方法是单独使用UITextView。 Since it is a descendant of UIScrollView, it has scrolling built-in and lots of features for adjusting formatting such as the insets etc. 由于它是UIScrollView的后代,因此它具有内置的滚动功能以及许多用于调整格式的功能,例如插图等。

If you intend to only show text, you need to explicitly disable editing. 如果您只想显示文本,则需要明确禁用编辑。 You do this by setting the "editable" property to false. 您可以通过将“editable”属性设置为false来执行此操作。 And if you want to disable the text selection mechanism, set the "selectable" property to false. 如果要禁用文本选择机制,请将“selectable”属性设置为false。

In newer versions of iOS, UITextView has added support for NSTextContainer which gives you even greater control over formatting. 在较新版本的iOS中, UITextView增加了对NSTextContainer的支持,使您可以更好地控制格式。

Usage of the UITextView into the UIScrollView. 将UITextView用于UIScrollView。 I could not recommend this because UITextView is the subclass of UIScrollView. 我不推荐这个,因为UITextView是UIScrollView的子类。 Apple is also recommending the same. Apple也推荐相同的产品。

Use UILabel in this case as a sub-view, 在这种情况下使用UILabel作为子视图,

将UITextView放入UIScrollView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM