detailTextLabel textalignment - PullRequest
       4

detailTextLabel textalignment

0 голосов
/ 06 июля 2011

Я пытаюсь отцентрировать текст с подробностями ячейки.

Я прочитал все виды сообщений на эту тему, но, похоже, все они говорят о старых версиях IOS.Я думаю, что я попробовал каждую комбинацию сообщений, но не повезло, что она заработала.

[[cell detailTextLabel] setTextAlignment:UITextAlignmentCenter];

Я попробовал это из willDisplayCell, а также в приведенном ниже коде, ни одна из них не работает.Обратите внимание, 2 способа сделать это я попробовал в обоих методах.

Кто-нибудь знает, работает ли это или я должен создать свою собственную функцию (метод) центра?

static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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


    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0];


    cell.detailTextLabel.font = [UIFont  systemFontOfSize:16];


    NSMutableDictionary *curRow = [myData objectAtIndex:indexPath.row];
    cell.textLabel.text = [curRow objectForKey:@"Description"];

    cell.detailTextLabel.text = [curRow objectForKey:@"Stats"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.detailTextLabel.textAlignment = UITextAlignmentCenter;

Ответы [ 2 ]

2 голосов
/ 19 октября 2011

В качестве альтернативы, если вы хотите центрировать detailTextLabel, все еще используя автоматический вертикальный центр (textLabel будет центрирован по вертикали, если detailTextLabel пуст), вам необходимо переопределить - (void) layoutSubviews.

В противном случае размер метки будет соответствовать содержимому, и поэтому textAlignment = UITextAlignmentCenter не будет работать.

- (void) layoutSubviews
{
    [super layoutSubviews];
    self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.frame.size.width, self.textLabel.frame.size.height);
    self.detailTextLabel.frame = CGRectMake(0, self.detailTextLabel.frame.origin.y, self.frame.size.width, self.detailTextLabel.frame.size.height);
}
2 голосов
/ 06 июля 2011

Если выравнивание является проблемой для вас, тогда вы можете создавать собственные метки и добавлять подпредставление в ячейку.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    UILabel *label;
    UILabel *detailLabel;

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        label = [[[UILabel alloc] initWithFrame:CGRectMake(55, 4, 260, 20)] autorelease];
        //make Your alignments to this label
        label.font = [UIFont boldSystemFontOfSize:15.0];
        label.tag=25;

        //make Your alignments to this detail label
        detailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(55, 25, 260, 15)] autorelease];
        detailLabel.font = [UIFont systemFontOfSize:13.0];
        detailLabel.tag=30;
        [cell.contentView addSubview:label];
        [cell.contentView addSubview:detailLabel];
    }
    else
    {
        label = (UILabel *)[cell.contentView viewWithTag:25];
        detailLabel = (UILabel *)[cell.contentView viewWithTag:30];
    }
    label.text =[curRow objectForKey:@"Description"];
    detailLabel.text=[curRow objectForKey:@"Stats"];
    return cell;
}
...