【发布时间】:2023-03-25 11:24:02
【问题描述】:
如何在自定义表格单元格类中绘制矩形?该单元格当前有一个带有一些文本标签的背景图像。我想在每个标签后面画一个矩形,以便更容易阅读详细的背景图像。
我知道我可以只设置标签的背景颜色,但我想在背景颜色和文本之间设置填充。如果可能的话,我很想知道怎么做! :)
我在 Three20 中对 TTTableMessageItemCell 进行子类化,调用下面的方法,您可以在其中使用单元格的子视图,
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat padding = 16;
CGFloat boxWidth = self.contentView.width - 2*padding;
CGFloat textWidth = boxWidth - (padding*2);
CGFloat textHeight = 100;
CGFloat top = kTableCellSmallMargin;
// Position Heading Text
_titleLabel.frame = CGRectMake(padding, top, textWidth, _titleLabel.font.ttLineHeight);
top += _titleLabel.height;
// Position Detail Text
[self.detailTextLabel sizeToFit];
self.detailTextLabel.top = top+2*padding;
self.detailTextLabel.left = 2*padding;
self.detailTextLabel.width = textWidth;
self.detailTextLabel.height = 100;
}
我希望将矩形放在 _titleLable 和 detailTextLabel 标签后面。
编辑 我已经能够使用以下方法添加正确的框,
UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor whiteColor]; view.frame = CGRectMake(padding, top, textWidth, textHeight+2*padding); [self insertSubview:view belowSubview:self.detailTextLabel];
它位于标签之上,我似乎无法将它放在它后面......
编辑 我将视图添加到错误的子视图中,修复了它,
[[self.subviews objectAtIndex:0] insertSubview:view atIndex:0];
【问题讨论】:
标签: iphone objective-c cocoa-touch tablecell