Text alignment NSTextAlignmentRight in UITableViewHeaderFooterView's textLabel for table view section header not working.

I'm trying to right align the text that is shown up in the table view section header from its datasource call viewForHeaderInSection using the following code but the text is still left aligned. I tried other alignment options but none worked. Can someone help me out please.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(legendTableView.frame.origin.x, legendTableView.frame.origin.y, legendTableView.frame.size.width, 10)];
    headerView.textLabel.textAlignment = NSTextAlignmentRight;
    double total= 0;
    for (Amount *amountObj in currentArray) {
        total=total+[amountObj.amount doubleValue];
    }
    NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
    currencyFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
    NSString *amountString = [currencyFormatter stringFromNumber:[NSNumber numberWithDouble:total]];
    NSString *totalString = [NSString stringWithFormat:@"Total: %@",amountString];
    headerView.textLabel.text = totalString;
    return headerView;
}

Thanks. Neerav

Your textLabel likely is right aligned, but it doesn't look like it because the UILabel is sized just wide enough for the text, and the UILabel itself is left aligned. Instead, you'll need to use auto layout constraints (or autoresizing masks) to make the UILabel as wide (or nearly as wide, with some margins) as the header view.