smallgood
smallgood

Reputation: 39

How to change the linespace in the TableViewCell

Everyone, I need insert some texts to the tableView cell. But i don't know how to change the linespace. So it looks ugly. Because every line too close. Please help me. Thanks very very much!

//texts
UITextView *hisText = [[UITextView alloc] init];
hisText.frame = CGRectMake(100, 110, 600, 300);
[hisText setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];
hisText.textColor = [UIColor blackColor];
hisText.editable = NO;

switch (indexPath.row) {
    case 0:
        hisText.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
        hisImg.image = [UIImage imageNamed:@"1928-1.jpg"];
        [cell.contentView addSubview:hisText];
        //[cell.contentView addSubview:hisImg];
        break;
    case 1:
        hisText.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
        [cell.contentView addSubview:hisText];
        break;
    default:
        break;
}

Upvotes: 0

Views: 138

Answers (3)

JustAnotherCoder
JustAnotherCoder

Reputation: 2575

IF the length of all your text will fit in that UITextView frame you create, then you can simply do this in heightForRowAtIndexPath:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
      return 110; //you can set to whatever height you wish if you want some 'padding'
 }  

HOWEVER, one correct way to do this so that it will fit any length of text would be to create the UITableView in heightForRow and size it properly to fit the text (perhaps using sizeToFit) and then taking it's tableView.frame.size.height and returning that value for the tableView row height.

Something like:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   UITextView *hisText = [[UITextView alloc] init];
   hisText.frame = CGRectMake(100, 110, 600, 300);
   [hisText setFont:[UIFont fontWithName:@"HelveticaNeue" size:14]];

   switch (indexPath.row) {
    case 0:
        hisText.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium    adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
         [hisText sizeToFit];
         break;
    case 1:
        hisText.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.";
        [hisText sizeToFit];
        break;
    default:
        break;
    }

    float theHeight = hisText.frame.size.height;

    return theHeight;

}

Upvotes: 1

chandru
chandru

Reputation: 417

U can increase the height of the row.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [indexPath row]+110;
}  

Here , [indexPath row]- has the default value of the rowheight

Upvotes: 0

Madhu
Madhu

Reputation: 1542

1) Instead of UITextView take UILabel and as per size of the text adjust the frame of UILabel.

Now if you want line space increase the frame of UILabel as per your requirement, it will give you space between the lines.

2) Or take UIWebeview instead of UITextView and in that you can provide line space as per HTML body.

Upvotes: 0

Related Questions