【问题标题】:How can I change a cell's textLabel frame?如何更改单元格的 textLabel 框架?
【发布时间】:2011-01-28 04:55:00
【问题描述】:

我几乎尝试了所有方法,但似乎无法将 cell.textLabel 属性向下移动一点。我在下面附上了一个截图,我已经尝试了几乎所有我能找到的东西。

我尝试直接更改 .frame 属性,尝试通过使用“- (void)tableView:(UITableView *)tableView willDisplayCell” 方法进行修改。我还尝试分配自定义标签。我可以移动自定义标签,但它不会像原始 textLabel 那样进入单独的行。我只需将图中的多行标签向下移动一点。

感谢任何帮助!

【问题讨论】:

    标签: iphone ios uilabel


    【解决方案1】:

    为您的 UITableViewCell 覆盖 layoutSubviews...

    - (void)layoutSubviews {
        [super layoutSubviews];
    
        CGSize size = self.bounds.size;
        CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height); 
        self.textLabel.frame =  frame;
        self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
    }
    

    或者您可以简单地创建自己的 UILabel 对象,并将其作为子视图添加到 cell.contentView。

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 30, 30)];
    [cell.contentView addSubview:label];
    [label release];
    

    【讨论】:

      【解决方案2】:

      这样做的唯一方法是使用UITableViewCell 子类并覆盖-layoutSubviews。在此方法中,您需要调用[super layoutSubviews],然后对标签进行任何您想要的框架调整。

      【讨论】:

        【解决方案3】:

        我最终在字符串的开头添加了一个 \n。不幸的是,我无法进行其他任何工作。

        【讨论】:

          【解决方案4】:
          - (void)layoutSubviews {
              [super layoutSubviews];
          
              CGSize size = self.bounds.size;
              CGRect frame = CGRectMake(4.0f, 4.0f, size.width, size.height); 
              self.textLabel.frame =  frame;
          
              self.textLabel.textAlignment = NSTextAlignmentCenter;
          }
          

          【讨论】:

          • 你能解释一下你的代码是做什么的,为什么它回答了这个问题吗?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-13
          • 2011-06-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-23
          相关资源
          最近更新 更多