1

My UILabel is in UITableViewCell and Here is my code

lblgoal = [[UILabel alloc] initWithFrame:CGRectMake(60,10, 250, 20)];
CGSize maximumLabelSize1 = CGSizeMake(230,9999);
            lblgoal.numberOfLines = 0;
            lblgoal.adjustsFontSizeToFitWidth = NO;
            CGSize expectedLabelSize = [sg.subgoal_name sizeWithFont:lblgoal.font
                                                   constrainedToSize:maximumLabelSize
                                                   lineBreakMode:lblsubgoal1.lineBreakMode];
CGRect newFrame = lblgoal.frame;
            newFrame.size.height = expectedLabelSize.height;
            lblgoal.frame = newFrame;

But this only works if i put the width of UILabel to 200, if i put more than 200 width, then it all comes in one line with dotted at the end. And, UITableView cell width is more than 500.

000
  • 26,951
  • 10
  • 71
  • 101

3 Answers3

1

Actually, make sure to set the linebreakmode to UILineBreakModeWordWrap in both your sizeWithFont function and your lblgoal label as below:

lblgoal.numberOfLines = 0;
lblgoal.lineBreakMode = UILineBreakModeWordWrap; //NSLineBreakByWordWrapping for iOS 6

EDIT: Try sizeToFit after you set the lineBreakMode.

[lblgoal sizeToFit];

Also make sure you do this inside the cellForRowAtIndexPath method.

You should also implement the following delegate method to make sure the height of your cell is also adjusted accordingly.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
kushyar
  • 1,191
  • 1
  • 6
  • 10
  • by the way in ios 6 its, NSLineBreakByWordWrapping, but this doesn't work either, thanks – user1495387 Jun 03 '13 at 09:17
  • you are correct about NSLineBreakByWordWrapping if your target is iOS 6. try sizeToFit. That is the simplest solution for UITableViewCell aside from subclassing UITableViewCell – kushyar Jun 03 '13 at 09:26
  • when i use sizetofit, the whole text inside label disappear – user1495387 Jun 03 '13 at 09:50
  • hey i think adding lblgoal.lineBreakMode = NSLineBreakByWordWrapping; after numberoflines work, thanks a lot for saving my time – user1495387 Jun 03 '13 at 09:55
0

You have a fixed Width when you are checking for the MaximumLabelSize.

CGSize maximumLabelSize1 = CGSizeMake(230,9999);

Instead Use

CGSize maximumLabelSize1 = CGSizeMake(MAXFLOAT, MAXFLOAT);

But this will give the Oppurtunity to extend width to any extend. Which is not going to work for u.. So set the Maximum Width to 500.

CGSize maximumLabelSize1 = CGSizeMake(500, MAXFLOAT);

And set the Frame for the Label :

CGRect newFrame = lblgoal.frame;
newFrame.size.height = expectedLabelSize.height;
newFrame.size.width = expectedLabelSize.width;
Roshit
  • 1,589
  • 1
  • 15
  • 37
0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
     if (!self.lastIndexPath) 
     {
        self.lastIndexPath = indexPath;
     }

    if ([self.lastIndexPath row] != [indexPath row])
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;

        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath]; 
        oldCell.accessoryType = UITableViewCellAccessoryNone;

        self.lastIndexPath = indexPath;  
    }
    else {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
Mital
  • 241
  • 1
  • 5