代码之家  ›  专栏  ›  技术社区  ›  Alexandru

我正在尝试缩进表视图单元格文本标签的右边距

  •  0
  • Alexandru  · 技术社区  · 8 年前

    我已使用以下代码从左侧缩进单元格:

    [cell setIndentationWidth:1];
    [cell setIndentationLevel:15];
    

    不幸的是,当单元格包含大量文本时,这会偏移表视图单元格文本标签,因此这里的问题是,省略号是文本标签的内容更右侧,而从左侧缩进时省略号显示得比通常要远。

    我如何才能 UITableViewCell textLabel 还有右缩进(或边距)吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Alexandru    8 年前

    我做了以下操作(似乎您不能以任何其他方式设置文本标签的框架)。

    标题(.h):

    @interface UITableViewCellFixed : UITableViewCell
    
    @property (nonatomic, assign) CGRect textLabelFrame;
    
    @end
    

    实现(.m):

    @implementation UITableViewCellFixed : UITableViewCell
    
    - (id)initWithLabelFrame:(CGRect)textLabelFrame {
        self = [super init];
        if (self)
            self.textLabelFrame = textLabelFrame;
        return self;
    }
    
    - (void) layoutSubviews {
        [super layoutSubviews];
        self.textLabel.frame = self.textLabelFrame;
    }
    
    @end
    
    ...
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ...
        UITableViewCellFixed *cell = [[UITableViewCellFixed alloc] initWithLabelFrame:CGRectMake(30, 0, tableView.frame.size.width - 60, 50)];
        ...
    }