UITableView titleForSection字体

cam*_*ilo 8 iphone xcode interface-builder uitableview nsstring

一个快速的问题,快速回答(因为我找不到):

有没有办法在iPhone中更改部分标题的字体(由titleForSection给出)?

非常感谢!

cam*_*ilo 39

谢谢Jasarien!你是对的.

我将代码保留在这里以帮助遇到同样问题的人:

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

   NSString *sectionTitle = @"Just a title";

    // Create label with section title
    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.frame = CGRectMake(0, 0, 284, 23);
    label.textColor = [UIColor blackColor];
    label.font = [UIFont fontWithName:@"Helvetica" size:14];
    label.text = sectionTitle;
    label.backgroundColor = [UIColor clearColor];

    // Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    [view autorelease];
    [view addSubview:label];

    return view;
}
Run Code Online (Sandbox Code Playgroud)

  • 这很棒,但是,UILabel是UIView的直接子类,因此您不需要创建"标题"视图并将标签添加为子视图.您只需将标签本身作为标题视图返回即可. (10认同)
  • 如果你将标签本身作为标题返回,你会如何在左边给它一些偏移?在这种情况下更改标签框架的origin.x不起作用.. (5认同)

Jas*_*ien 26

您必须使用该viewForHeaderInSection:方法并提供自己的视图.幸运的是,这可以是具有指定字体的UILabel,因此您可以非常轻松地完成.


Aru*_*nGJ 5

你必须覆盖

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

为节标题提供所需的高度.否则它将与细胞重叠.