UITableView change header title color

别来无恙 提交于 2019-12-04 17:51:59

问题


I'm styling the UITableView in InAppSettingsKit and want to change the color of the header title:

The labels Without title and Text Field should be white. How can this be done?

Thanks.


回答1:


You can try following line of code in table cell creation method -

cell.textLabel.backgroundColor = //Your color;
cell.detailTextLabel.backgroundColor = //Your color;

You can refer following for more detail description, where you can find detail example of creating custom sectioned table similar to what you have mentioned - http://undefinedvalue.com/2009/08/25/changing-background-color-and-section-header-text-color-grouped-style-uitableview




回答2:


This is an old question, but I think the answer needs to be updated.

This method does not involve defining and creating your own custom view. In iOS 6 and up, you can easily change the background color and the text color by defining the

-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger)

delegate method.

For example:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    view.tintColor = [UIColor blackColor];

    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    [header.textLabel setTextColor:[UIColor whiteColor]];

    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
}

Taken from my post here: https://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/

Swift 5.0:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let header = view as? UITableViewHeaderFooterView {
        header.textLabel?.textColor = .white
    }
}



回答3:


Implement the tableView:viewForHeaderInSection: method in the tableViewController. That will allow you to supply your own view for the headers, which can include a UILabel with whatever formatting you want, e.g.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *customLabel = [[UILabel alloc] init];
    customLabel.text = [self tableView:tableView titleForHeaderInSection:section];
    return customLabel;
}

Remember to set the label frame to be tall enough to space out the sections. You may wish to embed the label inside a larger UIView and return that instead to simplify positioning (e.g. if you want increase the left-padding on the label).




回答4:


It took me a few minutes to "translate" this to Swift, but here's a working equivalent in Swift 1.2 (iOS 8). Be sure and implement UITableViewDelegate in your class:

// MARK: - VIEW METHODS
override func viewDidLoad() {
    tableView.delegate = self
}

// MARK: - TABLEVIEW DELEGATE METHODS
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel.textColor = UIColor.whiteColor()
}



回答5:


I found this: How to change text color for Section Headers in a Grouped TableView in iPhone SDK? It works a treat, and I combined it with a little bit of code from the link in the answer by @rishi, and it made my life so much easier!! Although I had to tweak the coordinations of the label view a bit, but it worked like a charm.




回答6:


Swift 4 version:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    guard let headerView = view as? UITableViewHeaderFooterView else { return }
    headerView.textLabel?.textColor = .red // any color
}


来源:https://stackoverflow.com/questions/8990880/uitableview-change-header-title-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!