简体   繁体   English

如何更改iPhone SDK中Grouped TableView中的Section Headers的文本颜色?

[英]How to change text color for Section Headers in a Grouped TableView in iPhone SDK?

I am making an iPhone app where in I have a grouped TableView with headers for the sections. 我正在制作一个iPhone应用程序,其中我有一个分组的TableView与章节的标题。

Problem is that I want to change the Section Header's text color. 问题是我想要更改Section Header的文本颜色。

How can I change the text color of Section Header? 如何更改Section Header的文本颜色?

What should I do? 我该怎么办?

将以下代码添加到AppDelegate类中- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法:

[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];

If you don't want to do it app wide like in Vahan's solution, here is a solution using one of UITableViewDelegate 's method : 如果您不想像在Vahan的解决方案中那样广泛应用,那么这里有一个使用UITableViewDelegate方法的解决方案:

func tableView(tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
       headerView.textLabel?.textColor = UIColor.redColor() 
    }
}

This is SURELY gonna work for you. 这是SURELY会为你工作。

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor clearColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor]; 
    tempLabel.shadowColor = [UIColor blackColor];
    tempLabel.shadowOffset = CGSizeMake(0,2);
    tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:fontSizeForHeaders];
    tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
        tempLabel.text=@"Header Text";

    [tempView addSubview:tempLabel];

    [tempLabel release];
    return tempView;
}

just copy and paste this function in your code. 只需在代码中复制并粘贴此功能即可。

You can implement this table view data source method: 您可以实现此表视图数据源方法:

- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
   //create your custom label here & anything else you may want to add
   return YourCustomView;
}

@Harsh 's answer worked great for me, and by changing the coordinations of UILabel you can move it around. @Harsh的回答对我很有帮助,通过改变UILabel的协调,你可以移动它。 Also, I, personally thought to change the shadow offset a bit to make it more readable, but that could be a personal choice. 另外,我个人认为稍微改变阴影偏移以使其更具可读性,但这可能是个人选择。 Here's my version in case: 这是我的版本:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, -5, 300, 30)] autorelease];
    //If you add a bit to x and decrease y, it will be more in line with the tableView cell (that is in iPad and landscape)
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor yellowColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.5., 0.5.);
    label.font = [UIFont boldSystemFontOfSize:18];
    label.text = sectionTitle;

    // Create header view and add label as a subview
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)]autorelease];
    [view addSubview:label];

    return view;
}
 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  UILabel *myLabel = [[UILabel alloc] init];
  myLabel.frame = CGRectMake(20, 8, 220, 20);
  myLabel.font = [UIFont boldSystemFontOfSize:16];
  myLabel.text = [self tableView:tableView 
  titleForHeaderInSection:section];
  myLabel.backgroundColor=[UIColor grayColor];
  UIView *headerView = [[UIView alloc] init];
  [headerView addSubview:myLabel];
  return headerView;
  }

I built off of the answer from @Harsh. 我建立了@Harsh的答案。

This is the closest I could get, indistinguishable from what I can tell. 这是我能得到的最接近的,与我能说的无法区分。

It goes in the <UITableViewDataSource> obviously. 它显然在<UITableViewDataSource>中。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *hView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    hView.backgroundColor=[UIColor clearColor];

    UILabel *hLabel=[[[UILabel alloc] initWithFrame:CGRectMake(19,17,301,21)] autorelease];

    hLabel.backgroundColor=[UIColor clearColor];
    hLabel.shadowColor = [UIColor whiteColor];
    hLabel.shadowOffset = CGSizeMake(0.5,1);  // closest as far as I could tell
    hLabel.textColor = [UIColor blackColor];  // or whatever you want
    hLabel.font = [UIFont boldSystemFontOfSize:17];
    hLabel.text = @"Your title here";  // probably from array

    [hView addSubview:hLabel];

    return hView;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [iPhone]如何设置分组tableview部分的填充? - [iPhone]How to set padding of grouped tableview section? [iPhone]如何在分组TableView中自定义Section的标题? - [iPhone]How to customize header of Section in grouped TableView? 在保留纹理的同时更改iPhone tableview(样式分组)背景颜色 - Change iPhone tableview (style grouped) background color while preserving texture 如何在iphone sdk中自定义tableview的滚动颜色? - how to customize scroll color of tableview in iphone sdk? 当我们在iPhone SDK中有多个部分时如何更改同一部分中不同单元格的大小和颜色 - How to change size and color of different cell in same section when we r having multiple section in iphone sdk 如何在tableview(iPhone)的特定部分清除(分隔符颜色)? - How to clear (a separator color) in particular section in tableview (iPhone)? 更改 TableView 行 [iPhone SDK] - change TableView Lines [iPhone SDK] iPhone SDK中的如何更改BarButton项目的颜色 - in iPhone SDK How to change the color of BarButton Item 如何通过情节提要使用静态内容时,如何在分组的tableView的标头部分中更改字体样式 - How to change style of font in header section of grouped tableView while using static content via storyboard 表格视图中的分组单元格在下一节标题之前具有半透明区域/填充。 我该如何改变? - Grouped cell in a tableview has translucent area/padding before next section header. How do I change that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM