xcode 将大量文本添加到 UIScrollView 的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2486498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to add a large chunk of text to a UIScrollView?
提问by RexOnRoids
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? Length of the text is variable.
在 UIScrollView 中显示大块文本(取自应用程序中的 .txt 文件)以便用户可以滚动的最佳方式是什么?文本的长度是可变的。
采纳答案by Pripyat
Put the UITextView into the UIScrollView.
将 UITextView 放入 UIScrollView。
回答by HotFudgeSunday
On Interface Builder open the Attributes Inspector (if not already open - command-1) and uncheck "Editable".
在 Interface Builder 上打开 Attributes Inspector(如果尚未打开 - command-1)并取消选中“可编辑”。
Also notice there's a Scroll View section below. 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).
希望这对某人有所帮助(该帖子已有一年历史,所以我猜现在发布它的人不需要此信息)。
回答by Emmanuel Ay
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. Since it is a descendant of UIScrollView, it has scrolling built-in and lots of features for adjusting formatting such as the insets etc.
正确的方法是单独使用 UITextView。由于它是 UIScrollView 的后代,它具有内置滚动和许多用于调整格式的功能,例如插图等。
If you intend to only show text, you need to explicitly disable editing. You do this by setting the "editable" property to false. And if you want to disable the text selection mechanism, set the "selectable" property to false.
如果您打算只显示文本,则需要明确禁用编辑。您可以通过将“editable”属性设置为 false 来完成此操作。如果要禁用文本选择机制,请将“selectable”属性设置为 false。
In newer versions of iOS, UITextViewhas added support for NSTextContainerwhich gives you even greater control over formatting.
在较新版本的 iOS 中,UITextView添加了对NSTextContainer 的支持,这使您可以更好地控制格式。
回答by RealNmae
One way I had working for me is to create UILabel, set text and then set content size of scrollview by it size. Here is an example
我对我来说有效的一种方法是创建 UILabel,设置文本,然后通过它的大小设置滚动视图的内容大小。 这是一个例子
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];
回答by Jerald
Usage of the UITextView into the UIScrollView. I could not recommend this because UITextView is the subclass of UIScrollView. Apple is also recommending the same.
在 UIScrollView 中使用 UITextView。我不推荐这个,因为 UITextView 是 UIScrollView 的子类。苹果也推荐同样的。
Use UILabel in this case as a sub-view,
在这种情况下使用 UILabel 作为子视图,