2013-09-11 2 views
4

Я хочу, чтобы имитировать программно точный вид этого заголовка: enter image description hereiOS- имитируют системы по умолчанию заголовок Tableview

До сих пор моя лучшая попытка была:

UILabel* header = [[UILabel alloc] init] ; 
header.backgroundColor = [UIColor clearColor]; 
header.textAlignment = NSTextAlignmentCenter; 
header.textColor = [[UIColor alloc] initWithRed:86 green:92 blue:112 alpha:0.1]; 
header.shadowColor = [UIColor darkGrayColor]; 
header.shadowOffset = CGSizeMake(1,0); 
header.font = [UIFont boldSystemFontOfSize:18]; 

Но это выглядит следующим образом : enter image description here

Может кто-нибудь, пожалуйста, помогите мне с точным способом сделать это?

Заранее благодарен!

ответ

3

Norvegian парень написал в блоге об этом некоторое время назад: http://www.gersh.no/posts/view/default_styling_of_sections_headers_in_uitableview_grouped

enter image description here

Все кредиты идут к нему за кодом.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; 
    containerView.backgroundColor = [UIColor groupTableViewBackgroundColor]; 
    CGRect labelFrame = CGRectMake(20, 2, 320, 30); 
    if(section == 0) { 
     labelFrame.origin.y = 13; 
    } 
    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.font = [UIFont boldSystemFontOfSize:17]; 
    label.shadowColor = [UIColor colorWithWhite:1.0 alpha:1]; 
    label.shadowOffset = CGSizeMake(0, 1); 
    label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.000]; 
    label.text = [self tableView:tableView titleForHeaderInSection:section]; 
    [containerView addSubview:label]; 
    return containerView; 
} 
+0

Отличный ответ! Благодаря! – gran33

1

ЯВНО

Вы должны установить header.frame = CGRectMake(10,1, 100, 18);

ИЛИ

метод

Использование Datasource из UITableView

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return @"Header"; // just pass name of your hearder 
} 

EDITED:

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 2, 100, 18)]; 
    label.text= [self.listOfHeader objectAtIndex:section]; 
    label.backgroundColor=[UIColor clearColor]; 
    label.textAlignment=NSTextAlignmentLeft; 
    [view addSubview:label]; 
    return view; 
} 

А также добавить

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 20; 
} 
+0

Во-первых, 10x. Моя главная цель - выровнять текст справа налево, поэтому я делаю это программно, и из-за этого я не могу использовать titleForHeaderInSection. Я хочу создать вручную: /. – gran33

+0

@ gran33 - проверить отредактированный ответ :) – iPatel

0

Что вы можете сделать, чтобы вычислить ширину вашего UILabel динамически/программно. Используя этот код:

CGSize maximumLabelSize = CGSizeMake(296,9999); 
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
         constrainedToSize:maximumLabelSize 
         lineBreakMode:yourLabel.lineBreakMode]; 

Затем добавьте 10 пикселов в ширину, которую вы получите. Установите раму вашего UILabel. И, установите его следующим образом:

header.frame = CGRectMake(0.0,5.0,expectedLabelSize.width + 10.0,expectedLabelSize.height); 
header.textAlignment = NSTextAlignmentRight; 
Смежные вопросы