简体   繁体   English

如何更改单元格的textLabel框架?

[英]How can I change a cell's textLabel frame?

I've tried nearly everything, but I just can't seem to move the cell.textLabel property down a little bit. 我已经尝试了几乎所有的东西,但我似乎无法将cell.textLabel属性向下移动一点点。 I've attached a screenshot below and I've tried nearly everything I could find. 我在下面附上了截图,我已经尝试了几乎所有我能找到的东西。

I've tried changing the .frame property directly, attempted at modifying if via using the "- (void)tableView:(UITableView *)tableView willDisplayCell" method". I've also tried allocating a custom label. I could move the custom label, but it wouldn't go into separate lines like the original textLabel. I just need to move the pictured multi line label a bit down. 我已经尝试直接更改.frame属性,尝试修改如果通过使用“ - (void)tableView:(UITableView *)tableView willDisplayCell”方法“。我也尝试分配自定义标签。我可以移动自定义标签,但它不会像原始textLabel那样进入单独的行。我只需要将图示的多行标签稍微移动一下。

Any help is appreciated! 任何帮助表示赞赏!

在此输入图像描述

override layoutSubviews for your UITableViewCell ... 覆盖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;
}

or you can simply make your own UILabel object, and add it to cell.contentView as a subview. 或者您可以简单地创建自己的UILabel对象,并将其作为子视图添加到cell.contentView。

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

The only way to do this is to use a UITableViewCell subclass and override -layoutSubviews . 唯一的方法是使用UITableViewCell子类并覆盖-layoutSubviews In this method, you'll want to call [super layoutSubviews] , and then do any frame tweaks that you want to the label. 在此方法中,您将要调用[super layoutSubviews] ,然后对标签执行任何帧调整。

I ended up adding a \\n in the beginning of the string. 我最后在字符串的开头添加了一个\\ n。 Unfortunately I couldn't get anything else to work. 不幸的是我无法得到任何其他工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM