0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.section == 0) {

        cell.accessoryType = UITableViewCellAccessoryNone;

        if (indexPath.row == 0) {

            cell.textLabel.text = NSLocalizedString(@"Username", @"");
            account = [self createNewLabel];
            account.text = @"this is xyz doing some work in xyz, xyz is also working with me and  we are doing great work, We hope to continue good work in future and make this company in good position. I wanted to stayed in this company for long time to take  this company in high value";
            [cell.contentView addSubview:account];

        }
        if (indexPath.row == 1) {

            cell.textLabel.text = NSLocalizedString(@"Password", @"");
            contact = [self createNewLabel];
            [cell.contentView addSubview:contact];
        }
    }
    cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
    cell.textLabel.textColor = [UIColor blackColor];

    return cell;
}

How do i adjust account label text in cell content view, there will be many sections and many data differently

4

2 回答 2

1

Here you go

+ (CGFloat) getHeightOfString:(NSString*)string withSize:(CGFloat)size andWidth:(CGFloat)width
{
    CGSize boundingSize = CGSizeMake(width, CGFLOAT_MAX);

    CGSize stringSize = [string sizeWithFont:[UIFont systemFontOfSize:size] constrainedToSize:boundingSize  lineBreakMode:UILineBreakModeWordWrap];
    return stringSize.height;
}

The above method gives you height of a text, this height you can set to your label in cell. The method requires the width of the label, font size and the text.

For implementing the label heights dynamically you need to take care of height of the row. So while setting the rowHeight, in the delegate method of tableView you need to calculate the height over their and set. And also in cellForRow method you need to calculate the height and set.

You also need to calculate the number of lines dynamically, it will be usually

int numberOfLines = height/18; // or try height/19

It should work perfectly.

Thanks

于 2012-10-12T06:30:17.703 回答
0

Try this:

NSString *lblText = @"YOUR STRING";
    CGSize lblSize = [lblText sizeWithFont:[UIFont italicSystemFontOfSize:17] constrainedToSize:CGSizeMake(930, 500) lineBreakMode:UILineBreakModeWordWrap];
    CGRect frame = account.frame; 
        frame.size.height = lblSize.height;
        account.frame = frame;
于 2012-10-12T06:33:37.877 回答