I'm planning on loading multi-paragraph content from a text file and displaying it on the UI. I'll be loading from one of several text files and won't know ahead of time how long the text is going to be. My first thought was to use a UILabel inside a UIScrollView. However, it doesn't seem like UILabel can be made to expand based on the number of lines it contains. Is this correct?
我打算从文本文件加载多段内容并在UI上显示。我将从几个文本文件中的一个加载,并且不会提前知道文本将持续多长时间。我的第一个想法是在UIScrollView中使用UILabel。但是,似乎UILabel不能根据它包含的行数进行扩展。它是否正确?
If UILabel isn't the right tool for the job, what should I use? UIWebView? Will UIWebView handle the scrolling nicely for me?
如果UILabel不适合这项工作,我应该使用什么? UIWebView的? UIWebView能否很好地处理滚动?
2 个解决方案
#1
19
Check out UITextView
. You can place one inside a UIView, and the text view provides all the functionality of a scroll view, and more (it is a UIScrollView subsclas).
查看UITextView。您可以在UIView中放置一个,文本视图提供滚动视图的所有功能,以及更多(它是UIScrollView子类)。
#2
14
If you don't want to use a UITextView, which is scrollable so will accommodate whatever text you need, you can check out some methods to compute the size of a UILabel, for example, on the fly.
如果您不想使用可滚动的UITextView以便容纳您需要的任何文本,您可以查看一些方法来计算UILabel的大小,例如,即时。
This should get you started in the right direction: NSString has a few UIKit category methods
这应该让你开始正确的方向:NSString有一些UIKit类别方法
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size;
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode;
You call these methods on an NSString, passing in whatever font you want used as well as the maximum size. Usually, I will constrain the width but give a very large height so it can let me know what the best height is. These methods will return a CGSize that you can then use to set the frames of the UILabels. For example, if I know the maximum width that I can place the label in is 300 points wide, I will pass a CGSizeMake(300, 2000) for the constrained size.
您可以在NSString上调用这些方法,传入您想要使用的任何字体以及最大大小。通常情况下,我会限制宽度,但要给出一个非常大的高度,这样它就可以让我知道最佳高度是多少。这些方法将返回一个CGSize,然后您可以使用它来设置UILabels的帧。例如,如果我知道我可以放置标签的最大宽度是300点宽,我将为受限大小传递CGSizeMake(300,2000)。
#1
19
Check out UITextView
. You can place one inside a UIView, and the text view provides all the functionality of a scroll view, and more (it is a UIScrollView subsclas).
查看UITextView。您可以在UIView中放置一个,文本视图提供滚动视图的所有功能,以及更多(它是UIScrollView子类)。
#2
14
If you don't want to use a UITextView, which is scrollable so will accommodate whatever text you need, you can check out some methods to compute the size of a UILabel, for example, on the fly.
如果您不想使用可滚动的UITextView以便容纳您需要的任何文本,您可以查看一些方法来计算UILabel的大小,例如,即时。
This should get you started in the right direction: NSString has a few UIKit category methods
这应该让你开始正确的方向:NSString有一些UIKit类别方法
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size;
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode;
You call these methods on an NSString, passing in whatever font you want used as well as the maximum size. Usually, I will constrain the width but give a very large height so it can let me know what the best height is. These methods will return a CGSize that you can then use to set the frames of the UILabels. For example, if I know the maximum width that I can place the label in is 300 points wide, I will pass a CGSizeMake(300, 2000) for the constrained size.
您可以在NSString上调用这些方法,传入您想要使用的任何字体以及最大大小。通常情况下,我会限制宽度,但要给出一个非常大的高度,这样它就可以让我知道最佳高度是多少。这些方法将返回一个CGSize,然后您可以使用它来设置UILabels的帧。例如,如果我知道我可以放置标签的最大宽度是300点宽,我将为受限大小传递CGSizeMake(300,2000)。