将文本对齐到iphone中headerlabel的中心位置

War*_*ior 2 iphone alignment uilabel

我是iphone开发的新手.我在标题标签中显示了文本.现在我想将文本对齐到中心.我使用以下代码

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0,300.0,44.0)];

UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor brownColor];

headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:15];
headerLabel.frame = CGRectMake(0.0, 0.0, 500.0, 22.0);  
headerLabel.text = @"Hello"; // 
[customView addSubview:headerLabel];
return customView;
}
Run Code Online (Sandbox Code Playgroud)

"你好"应该显示在标题标签的中心.请帮帮我.谢谢.

Ale*_*lex 5

试试这个:

headerLabel.textAlignment = UITextAlignmentCenter;
Run Code Online (Sandbox Code Playgroud)