在自定义表格单元格中绘制矩形
如何在自定义表格单元格类中绘制矩形?该单元格当前有一个带有一些文本标签的背景图像。我想在每个标签后面画一个矩形,以便更容易阅读详细的背景图像。
我知道我可以设置标签的背景颜色,但我希望在背景颜色和文本之间有填充。如果可能的话,我很想知道如何! :)
我在 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 白色颜色]; view.frame = CGRectMake(padding, 顶部, textWidth, textHeight+2*padding); [self insertSubview:view belowSubview:self.detailTextLabel];
它位于标签顶部,我似乎无法将其放在后面...
编辑 我将视图添加到错误的子视图中,修复了它,
[[self.subviews objectAtIndex:0] insertSubview:view atIndex:0];
How would I draw a rectangle in a custom table cell class? The cell currently has a background image with a few text labels. I would like to draw a rectangle behind each of the labels so they are easier to read over the detailed background image.
I know I could just set the background colour of the label but I would like to have padding between the background colour and the text. If that is possible, I'd love to know how! :)
I'm subclassing a TTTableMessageItemCell in Three20, a method below gets called in which you can play with subviews of the cell,
- (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;
}
I would like the rectangles to be placed behind the _titleLable and detailTextLabel labels.
edit
I have been able to add the right box using the following,
UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor whiteColor]; view.frame = CGRectMake(padding, top, textWidth, textHeight+2*padding); [self insertSubview:view belowSubview:self.detailTextLabel];
It is laying on top of the label and I cant seem to get it behind it...
edit
I was adding the view to the wrong subview, fixed it with,
[[self.subviews objectAtIndex:0] insertSubview:view atIndex:0];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将标签添加到视图并将这些标签添加到单元格。
您可以使用
insertSubview:belowSubview :
在标签后面添加视图。有了背景颜色和正确的框架,他们就会做你想做的事。您还可以携带 详细标签到前面
You can add the labels to views and these to the cell.
You could use
insertSubview:belowSubview:
to add views behind your labels. With backgroundColor and the right frame they will do what you intend to.You can also bring detailLabel to front