首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >detailTextLabel文本对齐

detailTextLabel文本对齐
EN

Stack Overflow用户
提问于 2011-07-06 12:01:19
回答 2查看 6.3K关注 0票数 0

我正在尝试使单元格详细信息文本居中。

我读过关于这方面的各种帖子,但似乎都在谈论旧版本的IOS。我想我尝试了所有的帖子组合,但都没有成功。

代码语言:javascript
复制
[[cell detailTextLabel] setTextAlignment:UITextAlignmentCenter];

我在willDisplayCell和下面的代码中尝试过,两者都不起作用。注意2种我在两种方法中都尝试过的方法。

有没有人知道这是否有效,或者我是否应该创建自己的中心函数(方法)?

代码语言:javascript
复制
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;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-06 12:37:12

如果对齐有问题,您可以创建自定义标签并向单元格添加子视图。

代码语言:javascript
复制
- (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
EN

Stack Overflow用户

发布于 2011-10-19 16:59:24

或者,如果您希望在使用自动垂直居中的同时将detailTextLabel居中(如果detailTextLabel为空,则textLabel将垂直居中),则需要覆盖- (void) layoutSubviews

否则,标签的大小将适合内容,因此textAlignment = UITextAlignmentCenter将无法工作。

代码语言:javascript
复制
- (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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6591409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档