detailTextLabel textalignment

て烟熏妆下的殇ゞ 提交于 2020-01-03 05:47:29

问题


I am trying to get the cell detail text to be centered.

I have read all kinds of posts on this but they all seem to be talking about older versions of IOS. I think I tried every combination of posts but no luck in making it work.

[[cell detailTextLabel] setTextAlignment:UITextAlignmentCenter];

I tried this from willDisplayCell and also in the code below, neither works. Note 2 ways of doing this I tried in both methods.

Does anyone know if this does work or should I create my own center function (method)?

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;

回答1:


If Alignment is problem for you then you can create custom labels and add subview to the cell.

- (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;
}



回答2:


Alternatively if you want to center the detailTextLabel while still making use of the automatic vertical center (textLabel will be vertically centered, if the detailTextLabel is empty) you need to override - (void) layoutSubviews.

Otherwise the label's size will fit the content, and therefore textAlignment = UITextAlignmentCenter will not work.

- (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);
}


来源:https://stackoverflow.com/questions/6591409/detailtextlabel-textalignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!