Centering a textLabel vertically in a UITableViewHeaderFooterView from tableView: willDisplayHeaderView: forSection:

I am trying to vertically center a text label for tableViewHeader. Here is the relevant code from- (void) tableView: (UITableView*) tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section:

- (void) tableView: (UITableView*) tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if(![view isKindOfClass:[UITableViewHeaderFooterView class]]){
        return;
    }

    UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
    UILabel* label = tableViewHeaderFooterView.textLabel;

    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont rev_lightAppFontOfSize: 24.0f];
    label.textColor = [UIColor rev_colorWithHex: 0x858585];
    label.text = [label.text capitalizedString];

    label.center = tableViewHeaderFooterView.center; // label not in superview yet

    tableViewHeaderFooterView.layer.borderColor = [UIColor greenColor].CGColor;
    tableViewHeaderFooterView.layer.borderWidth = 2.0f;

    tableViewHeaderFooterView.contentView.layer.borderColor = [UIColor purpleColor].CGColor;
    tableViewHeaderFooterView.contentView.layer.borderWidth = 2.0f;

    label.layer.borderColor = [UIColor orangeColor].CGColor;
    label.layer.borderWidth = 2.0f;

}

the label object is not a subtask of tableViewHeaderFooterView, so the centering code does not work. Any thoughts?

+4
source share
4 answers

, , . , tableView: willDisplayHeader: , tableViewHeaderFooterView View . , - . :

- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger)  section
{
    UIView* view = [[UIView alloc] init];
    UILabel* label = [[UILabel alloc] init];

    label.text = [self tableView: tableView titleForHeaderInSection: section];
    label.textAlignment = NSTextAlignmentCenter;

    [label sizeToFit];
    label.translatesAutoresizingMaskIntoConstraints = NO;

    [view addSubview:label];

    [view addConstraints:
     @[[NSLayoutConstraint constraintWithItem:label
                                  attribute:NSLayoutAttributeCenterX
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:view
                                  attribute:NSLayoutAttributeCenterX
                                 multiplier:1 constant:0],
     [NSLayoutConstraint constraintWithItem:label
                                  attribute:NSLayoutAttributeCenterY
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:view
                                  attribute:NSLayoutAttributeCenterY
                                 multiplier:1 constant:0]]];

    return view;
}

, - !

+12

willDisplayHeaderView.

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if([view isKindOfClass:[UITableViewHeaderFooterView class]]){
        UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
        [tableViewHeaderFooterView.textLabel setTextAlignment:NSTextAlignmentCenter];
    }
}
+2
func configureHeaderView(header: UITableViewHeaderFooterView, forSection section: Int) {
switch section {
case 0:
  header.textLabel!.text = "BASES \(selectedBases)/\(menuItem.freeBases!.integerValue)"
case 1:
  header.textLabel!.text = "TOPPINGS \(selectedToppings)/\(menuItem.freeToppings!.integerValue)"
case 2:
  header.textLabel!.text = "PREMIUMS \(selectedPremiums)/\(menuItem.freePremiums!.integerValue)"
case 3:
  header.textLabel!.text = "DRESSINGS \(selectedDressings)/\(menuItem.freeDressings!.integerValue)"
default:
  break
}
}

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let headerView = view as? UITableViewHeaderFooterView
headerView!.textLabel!.frame = CGRectMake(0, 10, tableView.frame.size.width, 25)
headerView!.textLabel!.backgroundColor = UIColor(red: 230.0/255.0, green: 230.0/255.0, blue: 230.0/255.0, alpha: 1.0)
headerView!.textLabel!.font = UIFont(name: "BrandonGrotesque-Medium", size: 10)
headerView!.textLabel!.textColor = UIColor.blackColor()
headerView!.textLabel!.textAlignment = .Center
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if Array(sectionToAddOnsDictionary.keys).count < 2 {
  return nil
}
var headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier("MYOSHeaderView")
if headerView == nil {
  tableView.registerClass(UITableViewHeaderFooterView.classForCoder(), forHeaderFooterViewReuseIdentifier: "MYOSHeaderView")
  headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier("MYOSHeaderView")
}
configureHeaderView(headerView!, forSection: section)
return headerView
}
+1

Swift 3, - , if let,

 func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        if (view is UITableViewHeaderFooterView) {
            if let tableViewHeaderFooterView = view as? UITableViewHeaderFooterView {
            tableViewHeaderFooterView?.textLabel?.textAlignment = .center
            }
        }
    }
+1

Source: https://habr.com/ru/post/1526620/


All Articles