ios: best way to display variable-length, multi-line text

后端 未结 2 2053
庸人自扰
庸人自扰 2021-02-05 05:50

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

相关标签:
2条回答
  • 2021-02-05 06:31

    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.

    This should get you started in the right direction: NSString has a few UIKit category methods

    - (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.

    0 讨论(0)
  • 2021-02-05 06:38

    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).

    0 讨论(0)
提交回复
热议问题