1

Sooo... I'm having problems using the scrollview and the fit text label on iOS7/xcode5, and I'm getting frustrated with this problem because it is easy to handle this on iOS6/xcode4...

Here is the preview of my code...

ViewController.h

#import <UIKit/UIKit.h>
#import "ViewControllerOther.h"

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (strong, nonatomic) NSArray *meepo;
@property (strong, nonatomic) NSArray *icon;

@property (strong, nonatomic) NSArray *taunt;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableview setDataSource:self];
    [self.tableview setDelegate:self];

    self.meepo = [[NSArray alloc] initWithObjects:
                    @"Meepo No. 1",
                    @"Meepo No. 2",
                    @"Meepo No. 3",
                    @"Meepo No. 4",
                    @"Meepo No. 5",
                    nil];
    self.icon = [[NSArray alloc] initWithObjects:
                   [UIImage imageNamed:@"meepo.jpg"],
                   [UIImage imageNamed:@"meepo.jpg"],
                   [UIImage imageNamed:@"meepo.jpg"],
                   [UIImage imageNamed:@"meepo.jpg"],
                   [UIImage imageNamed:@"meepo.jpg"],
                   nil];

    self.taunt = [[NSArray alloc] initWithObjects:
                 @"Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, Meepo No. 1 reporting, ",
                 @"Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, Meepo No. 2 reporting, ",
                 @"Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, Meepo No. 3 reporting, ",
                 @"Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, Meepo No. 4 reporting, ",
                 @"Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, Meepo No. 5 reporting, ",
                 nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.meepo count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    NSString *meepos = [self.meepo objectAtIndex:indexPath.row];
    UIImage *icon = [self.icon objectAtIndex:indexPath.row];
    [cell.textLabel setText:meepos];
    cell.imageView.image = icon;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewControllerOther *other = [self.storyboard instantiateViewControllerWithIdentifier:@"Delegate"];
    other.meepono = [self.meepo objectAtIndex:indexPath.row];
    other.index = [self.taunt objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:other animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ViewControllerOther.h

#import <UIKit/UIKit.h>

@interface ViewControllerOther : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *meepo;
@property (weak, nonatomic) IBOutlet UILabel *taunt;
@property (weak, nonatomic) IBOutlet UIScrollView *scroller;

@property (nonatomic, strong) NSString *meepono;
@property (nonatomic, strong) NSString *index;

@end

ViewControllerOther.m (*)

#import "ViewControllerOther.h"

@interface ViewControllerOther ()

@end

static CGRect size;
static CGSize maxSize;
static CGRect labelRect;

@implementation ViewControllerOther

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.meepo setText:self.meepono];

    [self.scroller setScrollEnabled:YES];



    labelRect = [self.taunt frame];
}

- (void)viewWillAppear:(BOOL)animated
{

}

-(void)viewWillLayoutSubviews
{
    [self.taunt setNumberOfLines:0];
    [self.taunt setText:self.index];
    maxSize = CGSizeMake(self.taunt.frame.size.width, MAXFLOAT);
    size = [self.taunt.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.taunt.font} context:nil];

    labelRect.size.height = size.size.height;

    [self.taunt setFrame:labelRect];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)sample:(id)sender {
    [self.scroller setContentSize:CGSizeMake(320, 1000)];
}
@end

And here's the output

1st - http://i.imagefra.me/b52js8zk

2nd - http://i.imagefra.me/313js8zl

My target on the the 2nd view is to dynamically resize the height of the label depending on how long the text is, and also, at the same time, resize the height of the scrollview and make it scrollable...

Note: This is just a sample project that I made so I can ask these kind of questions.

Update 1:

Okay, so I manage to make the resize label work by transferring the code from viewDidLoad method to viewWillLayoutSubviews method, but unfortunately, when I put the [self.scroller setContentSize:CGSizeMake(320, 1000)]; the label return to its normal size. Am I missing something?

TrieNoir
  • 53
  • 1
  • 1
  • 8

1 Answers1

13

To make a UILabel to fit it's content use sizeToFit

1) set number of lines to zero yourLabel.numberOfLines = 0; // Important to do

2) after setting the text call sizeToFit [YourLabel sizeToFit];

3) This will dynamically resize the height of your label according to its content.

UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,320,100)];
testLabel.numberOfLines = 0;
testLabel.text = @"add your string here";
[testLabel sizeToFit];

Now you can use the height of label to set the ContentSize of scrollview dynamically.

[self.scroller setContentSize:CGSizeMake(self.scroller.frame.size.width,testLabel.frame.size.height)];

Hope this will help you..

superGokuN
  • 1,399
  • 13
  • 28
  • it works fine when I alloc init the UILabel with code. But if I use xib file it doesn't work. What should I do with the UILabel in the xib? – Daniel Qiu Feb 05 '14 at 07:15
  • 1
    Make an IBOutlet.. Connect it with the Object in the xib file.. then just set the numberOfLines and sizeToFit to it. – superGokuN Feb 05 '14 at 07:17
  • 2
    Thanks a lot. I found out it was because I put those codes in viewdidload which should be put in viewDidLayoutSubviews. – Daniel Qiu Feb 05 '14 at 07:25
  • Question updated... Transferring the code from viewDidLoad to viewDidLayoutSubviews did work... My problem now is that when I put the code `[self.scroller setContentSize:CGSizeMake(320, 1000)];`, the label size returns to normal... – TrieNoir Feb 05 '14 at 12:20
  • This doesn't work when your view rotates, a better answer is here, check out the accepted answer: http://stackoverflow.com/questions/15927086/uilabel-sizetofit-and-constraints/15927392 – netwire Aug 07 '14 at 11:25