ios 如何更改单元格的 textLabel 框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4824973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change a cell's textLabel frame?
提问by Raphael Caixeta
I've tried nearly everything, but I just can't seem to move the cell.textLabel property down a little bit. I've attached a screenshot below and I've tried nearly everything I could find.
我几乎尝试了所有方法,但似乎无法将 cell.textLabel 属性向下移动一点。我在下面附上了一个截图,我已经尝试了几乎所有我能找到的东西。
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!
任何帮助表示赞赏!
回答by EvilPenguin
override layoutSubviews
for your UITableViewCell
...
覆盖layoutSubviews
您的UITableViewCell
...
- (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];
回答by Lily Ballard
The only way to do this is to use a UITableViewCell
subclass and override -layoutSubviews
. In this method, you'll want to call [super layoutSubviews]
, and then do any frame tweaks that you want to the label.
做到这一点的唯一方法是使用UITableViewCell
子类并覆盖-layoutSubviews
。在此方法中,您需要调用[super layoutSubviews]
,然后对标签进行任何您想要的帧调整。
回答by Raphael Caixeta
I ended up adding a \n in the beginning of the string. Unfortunately I couldn't get anything else to work.
我最终在字符串的开头添加了一个 \n。不幸的是,我无法让其他任何事情发挥作用。
回答by Huaf22
- (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;
}