6

This is in my .m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.scrollView addSubview:self.contentView];
    self.scrollView.contentSize = self.contentView.bounds.size;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

    NSMutableArray *arr = [[NSMutableArray alloc]init];
    arr = [Singleton getArray];

    NSString *str = [arr componentsJoinedByString:@"\n"];
    summaryLabel.text = str;
}

This is in my .h

@interface TotalViewController : UIViewController
{
    UIScrollView *scrollView;
    UIView *contentView;

}
@property (nonatomic, retain) IBOutlet UIScrollView * scrollView;
@property (nonatomic, retain) IBOutlet UIView       * contentView;
@property (nonatomic,strong) IBOutlet UILabel       * summaryLabel;

My Label is connected to the View Controller, my contentView is connected to the View Controller, and my summaryLabel is connected to the View Controller. I need the label to scroll and it is not.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Johnny Cox
  • 1,873
  • 3
  • 17
  • 17

2 Answers2

28

A really simple answer, if you just want a single scrollable label, would be to use UITextView instead (reference). Disable editing, and you get a scrollable label.

(Taken almost verbatim from: how to add a scroll function to a UILabel)

Community
  • 1
  • 1
Dawson Toth
  • 5,580
  • 2
  • 22
  • 37
  • Wow I cant believe how easy that was. Thanks. – Johnny Cox Jul 03 '12 at 00:30
  • You guys who are thinking of using UITextView must be aware that is absolutely getting bugs. If you want scrollable, as well as setting the font-size/family, UITextView may get you staying up night. (especially if you are setting the text programmatically, not by storyboard.) See also http://stackoverflow.com/a/19168661/4493512 – KoreanXcodeWorker Apr 08 '16 at 13:17
11

UIScrollView won't scroll if it's contents are not larger than it's visible area. Considering you are assigning the text to the label after you've set contentSize of the scrollview, it is unlikely that this is happening correctly. I would try something like this...

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.scrollView addSubview:summaryLabel];

    .....

    summaryLabel.text = str;

    // Assuming your label has numberOfLines = 0, and you want to scroll vertical
    CGSize maxSize = CGSizeMake(summaryLabel.frame.size.width, CGFLOAT_MAX);
    CGSize labelSize = [summaryLabel sizeThatFits:maxSize];
    scrollview.contentSize = labelSize;
}
emdog4
  • 1,975
  • 3
  • 20
  • 25
Jerry Jones
  • 5,393
  • 2
  • 22
  • 14
  • 2
    This is the better answer. A UITextView always allows selection (editing can be disabled), selection can be disabled only if UserInteraction is disabled. However, disabling UserInteraction also disables the scrolling feature of the TextView. So, if you don't mind the user being able to select/highlight text (copy, define, etc.) then using a TextView might work for you. If you don't want any user selection, this is the better answer. – emdog4 Oct 31 '13 at 20:01
  • 1
    And if you DO want the selection, then using UITextView is the "better answer". I'd content that neither is better, out of context. It all depends on what you want to do. – Dawson Toth Aug 13 '15 at 03:07