UITableViewStyleGrouped 默认标题视图? [英] UITableViewStyleGrouped default header view?
问题描述
我正在尝试实现一个用于删除整个部分的控件,如果删除按钮位于标题中,而不是像 UIPopoverView 这样的叠加层,那么它在我的应用中看起来最好.
I'm trying to implement a control for deleting entire sections, and it would look best in my app if the delete button was in the header, as opposed to an overlay like a UIPopoverView.
在写这个问题的过程中,我找到了答案.很简单,一旦有了起点.
In the process of writing this question, I found the answer. Easy enough, once there's a starting point.
推荐答案
我从 这个博客只有两篇博文,都是 2010 年的.
然后我又回到这个网站 只是为了字体颜色,因为它更麻烦分开.
I got the bulk of the code from this blog which has only two posts, both from 2010.
Then I went back to this site just for the font color, since it's more trouble to break apart.
三个小问题,都与标签有关.
Three minor problems, all with the label.
- Font is too narrow
- Text color is too dark
- Label origin is wrong
默认字体是已知的,所以先来.
The default font is known, so that comes first.
label.font = [UIFont boldSystemFontOfSize:17.0];
接下来是颜色,因为这很容易.为此使用了图像编辑器的吸管工具.
Color is next, since that's easy. Used an image editor's Eyedropper tool for this.
label.textColor = [UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1];
// Is there a difference between alpha:1 and alpha:1.000?
然后是困难的部分.近似猜测,然后进行一些调整以获得完美匹配.
Then the hard part. A close guess, and then some tweaking for a perfect match.
label.frame = CGRectMake(54, 4, headerView.frame.size.width-20, 22);
现在我们有了一个与当前 Grouped 标头完美匹配的自定义实现.
And now we have a custom implementation that perfectly matches the current Grouped header.
完成代码:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
tableView.sectionHeaderHeight = headerView.frame.size.height;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(54, 4, labelSize.width, labelSize.height)];
[label setBackgroundColor:[UIColor clearColor]];
[label setFont:[UIFont boldSystemFontOfSize:17.0]];
[label setShadowColor:[UIColor whiteColor]];
[label setShadowOffset:CGSizeMake(0, 1)];
[label setText:[self tableView:tableView titleForHeaderInSection:section]];
[label setTextColor:[UIColor colorWithRed:0.298 green:0.337 blue:0.423 alpha:1.000]];
[headerView addSubview:label];
return headerView;
}
在找到正确的字体/颜色后找到这个SO答案我.哦,好吧.
Found this SO answer after finding the right font/color myself. Oh well.
对于允许有效无限数量文本的标题标签:
For a title label that allows an effectively unlimited amount of text:
// before label init
NSString *title = [self tableView:tableView titleForHeaderInSection:section];
NSUInteger maxWidth = headerView.frame.size.width-108;
CGSize labelSize = [title sizeWithFont:[UIFont systemFontOfSize:17.0]
constrainedToSize:CGSizeMake(maxWidth, CGFLOAT_MAX)];
if (labelSize.width < maxWidth) labelSize.width = maxWidth;
// after setFont:
[label setNumberOfLines:0];
这篇关于UITableViewStyleGrouped 默认标题视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!