2013-01-23 25 views
1

中心垂直的UILabel文本如何更改tableview中部分文本的位置?我用viewForHeaderInSection:第一部分,我創建有somwthing這樣的:在表視圖節頭

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

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)]; 
    label.backgroundColor = [UIColor blackColor]; 
    label.textColor = [UIColor whiteColor]; 
    label.font = [UIFont fontWithName:@"Gotham-Bold" size:17.0f]; 

    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.comlpetedFetchedResultsController sections] objectAtIndex:section]; 
    label.text = [@" " stringByAppendingString:[sectionInfo name]]; 
    return label; 
} 

它不事關什麼尺寸是的UILabel幀的參數,我總是得到相同的結果:

enter image description here

哪有我在UILabel垂直居中文本? 感謝

+0

你可以看到文字是黑色的UILabel的頂部,我希望把它中心(一點點下來,現在) – edzio27

+0

你有沒有嘗試在中心使用的標籤標籤的文本。 textAlignment = UITextAlignmentCenter – Jbryson

+0

您是否嘗試更改y? UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0,3,300,30)]; –

回答

3

把標籤在一個UIView然後返回視圖:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 30)]; 
[view addSubview:label]; 
return view; 

確保您實現以下方法:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

否則你的觀點將只是對細胞的頂部。

2

通過此鏈接進入:NSTextAlignment

而且代碼片段如下,可以幫助你

UILabel *myLabel = [[UILabel alloc] init]; 

    [myLabel setText:@"Abc"]; 
    [myLabel setTextAlignment:NSTextAlignmentCenter]; 
4

嘗試

[yourLabel setNumberOfLines:0]; 
[yourLabel sizeToFit]; 
[yourLabel setContentMode:UIViewContentModeCenter];