43

In Xcode, I created a UILabel which will autoresize depending on how many lines of text I put on it. But I don't want the UILabel's height to exceed a certain limit (240 in my example), the code goes like this:

NSString *text = @"imagine this is a huge wall of text\n\n\n"
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setNumberOfLines:0];
CGSize labelSize = [text sizeWithFont:myLabel.font constrainedToSize:CGSizeMake(280, 240) lineBreakMode:myLabel.lineBreakMode];
myLabel.frame = CGRectMake(0, 0, 280, labelSize.height);

This works fine when my text is within about 10-15 lines. But if I put in something like 40 lines of text, the extra lines of text will go beyond my UILabel and get cut off.

How can I add a scroll function to myLabel so that myLabel will still have a maximum height of 240, and I can simply scroll down to view those extra lines of text in myLabel?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shakesbeer
  • 513
  • 1
  • 7
  • 12

2 Answers2

130

Use UITextView (reference).

It's designed to do exactly that. Disable editing, and you get a scrollable label.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
  • 5
    Be aware that UITextView is not a drop-in replacement for UILabel. For example (as of iOS 6), when you set a font on a UILabel with attributed text, that change "sticks"; when you set a font on a UITextView, the change only applies to whatever attributed is already on the UITextView -- if you assign a new attributed string, the font you previously set won't be reflected. (This was my experience working with this tonight, at least.) – Jon Schneider Sep 11 '13 at 03:34
  • 1
    @JonSchneider is UITextView still the "right" thing to use today? – kemicofa ghost Nov 07 '16 at 15:37
  • @rugdealer I'm afraid I can't give you a solid answer offhand; I haven't done serious iOS development since late 2014. – Jon Schneider Nov 07 '16 at 15:50
1

You can add your UILabel on a UIScrollView Like this and can also do like this---

scrollView.backgroundColor = [UIColor clearColor];

        UILabel * label = [[UILabel alloc] init];
        [label setNumberOfLines:0];
        label.text=[detailDict valueForKey:@"ABC"];
        [label setFont:[UIFont fontWithName:@"Georgia" size:16.0]];
        CGSize labelsize=[label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(250, 1000.0) lineBreakMode:UILineBreakModeWordWrap];
        int y=0;
        label.frame=CGRectMake(38, y, 245, labelsize.height);
        [label setBackgroundColor:[UIColor clearColor]];
        [label setTextColor:[UIColor whiteColor]];
        scrollView.showsVerticalScrollIndicator = NO;
        y+=labelsize.height;
        [scrollView setContentSize:CGSizeMake(200,y+50)];
        [scrollView addSubview:label];
        [label release];

Here Y is used to increase or decrease the label size as the text.

shivangi
  • 187
  • 2
  • 14
  • 3
    It is always a wrong way of doing things if you have better options. :( – Munim Jun 22 '13 at 06:59
  • 1
    This is actually an excellent solution to those of you who have grown tired of the growing number of issues with UITextView scrolling behavior. Especially when mixed with transparent navigation bars. Sometimes it scroll sometimes it doesn't. This solution works in places a UITextView will not. -rrh – Richie Hyatt Nov 04 '15 at 00:38