【问题标题】:Separate font and color of words in grouped header UItableview分组标题UItableview中单词的字体和颜色分开
【发布时间】:2012-09-11 05:34:37
【问题描述】:

我想在分组表视图的标题中显示三个字符串。问题是我在tableView titleForHeaderInSection 中附加了这个字符串,我想用不同的颜色和字体在不同的行中显示每个字符串。

我目前拥有的是:

我想要的是:

我得到并附加这三个字符串如下

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger) section 
{

    NSString * presenter = [[self agenda] getBriefingPresenter:section];
    NSString * time = [[self agenda] getBriefingTime:section];
    NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:@"subject"];

    return [NSString stringWithFormat:@"%@\n%@  %@", subject, time, presenter ];

}

页眉字体处理方法

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

    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(45, 0, 484, 23);
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"Century Gothic" size:17];
    label.text = sectionTitle;
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 2;
    [label sizeToFit];
    label.backgroundColor = [UIColor clearColor];

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

    return view;
}

那么我如何解析这个sectionTitle 并在三行中打印为三个不同的标签?

【问题讨论】:

    标签: ios uilabel uifont uitableview


    【解决方案1】:

    (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 中,创建3 个具有特定字体和颜色的不同标签。您不能在单个 UILabel 上设置不同的字体/颜色;

    所以你的代码更新了:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
        NSString * presenter = [[self agenda] getBriefingPresenter:section];
        NSString * time = [[self agenda] getBriefingTime:section];
        NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:@"subject"];
    
        // Create label with section title
        UILabel *presenterLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 484, 23)];
        presenterLabel.textColor = presenterColor;
        presenterLabel.font = presenterFont;
        presenterLabel.text = presenter;
        presenterLabel.backgroundColor = [UIColor clearColor];
        [presenterLabel sizeToFit];
    
        UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, presenterLabel.frame.size.height, 484, 23)];
        timeLabel.textColor = timeColor;
        timeLabel.font = timeFont;
        timeLabel.text = time;
        timeLabel.backgroundColor = [UIColor clearColor];
        [timeLabel sizeToFit];
    
        UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, timeLabel.frame.origin.y + timeLabel.frame.size.height, 484, 23)];
        subjectLabel.textColor = subjectColor;
        subjectLabel.font = subjectFont;
        subjectLabel.text = subject;
        subjectLabel.backgroundColor = [UIColor clearColor];
        [subjectLabel sizeToFit];
    
    
        // Create header view and add label as a subview
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
        [view addSubview:presenterLabel];
        [view addSubview:timeLabel];
        [view addSubview:subjectLabel];
    
        return view;
    }
    

    你不需要- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger) section

    【讨论】:

    • 谢谢,但是当我删除 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger) section 时,标题或标题区域缩小为零,并且 beetwee 分组表视图没有间隙
    • 你只需要继承-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section 并返回正确的高度
    • 刚刚添加了- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger) section { return [NSString stringWithFormat:@"\n \n \n"]; } 它有效,但我不知道它是否适合我
    • 正如我所说,删除它是肮脏的编码!而是添加这个-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section { return theHeightYouWantForYourHeader; }
    猜你喜欢
    • 2011-10-29
    • 1970-01-01
    • 2010-10-23
    • 2011-03-18
    • 1970-01-01
    • 2012-10-07
    • 2015-07-26
    • 2013-01-03
    • 2023-03-04
    相关资源
    最近更新 更多