如何将titleForHeaderInSection的文本设置为AlignmentCenter [英] how to set text of titleForHeaderInSection to AlignmentCenter

查看:67
本文介绍了如何将titleForHeaderInSection的文本设置为AlignmentCenter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码在组tableview标题标题处设置标题,但默认文本为AlignmentLeft,如何对齐AlignmentCenter?

i used the follow code to set the title at the group tableview title header,but default the text is AlignmentLeft,how to AlignmentCenter?

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if(section==0){
        return NSLocalizedString(@"more_titlehead_one", nil);
    }else if (section==1){
        return NSLocalizedString(@"more_titlehead_two", nil);
    }else{
        return NSLocalizedString(@"more_titlehead_three", nil);
    }


}

推荐答案

尝试这样,

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
     UILabel * sectionHeader = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
     sectionHeader.backgroundColor = [UIColor clearColor];
     sectionHeader.textAlignment = UITextAlignmentCenter;
     sectionHeader.font = [UIFont boldSystemFontOfSize:10];
     sectionHeader.textColor = [UIColor whiteColor];

     switch(section) {
        case 0:sectionHeader.text = @"TITLE ONE"; break;
        case 1:sectionHeader.text = @"TITLE TWO"; break;
        default:sectionHeader.text = @"TITLE OTHER"; break;
     }  
   return sectionHeader;
}

为页眉设置一些默认高度,

set some default height for Header,

- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
     switch(section) {
        case 0:
        case 1:
        default:return 20;
     }  
}

这篇关于如何将titleForHeaderInSection的文本设置为AlignmentCenter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆