【问题标题】:iOS- mimic system default tableView headeriOS-模仿系统默认的tableView header
【发布时间】:2013-09-15 06:15:45
【问题描述】:

我想以编程方式模仿此标头的确切外观:

到目前为止,我最好的尝试是:

UILabel* header = [[UILabel alloc] init] ;
header.backgroundColor = [UIColor clearColor];
header.textAlignment = NSTextAlignmentCenter;
header.textColor = [[UIColor alloc] initWithRed:86 green:92 blue:112 alpha:0.1];
header.shadowColor = [UIColor darkGrayColor];
header.shadowOffset = CGSizeMake(1,0);
header.font = [UIFont boldSystemFontOfSize:18];

但它看起来像这样:

有人可以帮我具体的方法吗?

提前致谢!

【问题讨论】:

    标签: iphone ios uitableview uilabel


    【解决方案1】:

    一位挪威人不久前写了一篇关于此的博客文章:http://www.gersh.no/posts/view/default_styling_of_sections_headers_in_uitableview_grouped

    代码的所有功劳归他所有。

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
        containerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        CGRect labelFrame = CGRectMake(20, 2, 320, 30);
        if(section == 0) {
            labelFrame.origin.y = 13;
        }
        UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:17];
        label.shadowColor = [UIColor colorWithWhite:1.0 alpha:1];
        label.shadowOffset = CGSizeMake(0, 1);
        label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.000];
        label.text = [self tableView:tableView titleForHeaderInSection:section];
        [containerView addSubview:label];
        return containerView;
    }
    

    【讨论】:

    • 很好的答案!谢谢!
    【解决方案2】:

    任一

    你需要设置header.frame = CGRectMake(10,1, 100, 18 );

    使用UITableView的Datasource方法

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return @"Header"; // just pass name of your hearder
    }
    

    已编辑:

    -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
       UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
       UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 2, 100, 18)];
       label.text= [self.listOfHeader objectAtIndex:section];
       label.backgroundColor=[UIColor clearColor];
       label.textAlignment=NSTextAlignmentLeft;
       [view addSubview:label];
       return view;
    }
    

    还有添加

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 20;
    }
    

    【讨论】:

    • 首先,10 倍。我的主要目标是将文本从右到左对齐,因此我以编程方式执行此操作,因此,我不能使用 titleForHeaderInSection。我想手工设计:/.
    【解决方案3】:

    你可以做什么动态/编程计算你的 UILabel 的宽度。使用此代码:

    CGSize maximumLabelSize = CGSizeMake(296,9999);
    CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                            constrainedToSize:maximumLabelSize 
                            lineBreakMode:yourLabel.lineBreakMode]; 
    

    然后将 10 像素添加到你得到的宽度。设置 UILabel 的框架。并且,设置如下:

    header.frame = CGRectMake(0.0,5.0,expectedLabelSize.width + 10.0,expectedLabelSize.height);
    header.textAlignment = NSTextAlignmentRight;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 2020-06-23
      • 2019-04-08
      相关资源
      最近更新 更多