2

我正在尝试子类化,出于某种原因,我遇到了三个问题。

首先是创建透明背景。第二个是更改textLabels 字体。第三个是为底部创建一个边框drawRect:(绿色只是一个测试器)

这是我所有的子类的代码:

+ (EVSectionHeaderView *)sectionHeaderViewWithSectionHeaderViewStyle:(SectionHeaderViewStyle)sectionHeaderViewStyle title:(NSString *)title
{
    EVSectionHeaderView *sectionHeaderView = [[EVSectionHeaderView alloc] init];

    UIView *backgroundView = [[UIView alloc] initWithFrame:sectionHeaderView.frame];
    [sectionHeaderView setBackgroundView:backgroundView];

    switch (sectionHeaderViewStyle) {
        case SectionHeaderViewStylePaper:
            [sectionHeaderView.backgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Paper Texture"]]];
            break;

        default:
            break;
    }

    [sectionHeaderView.layer setOpacity:0.1f];

    [sectionHeaderView.textLabel setText:title];
    [sectionHeaderView.textLabel setFont:[UIFont fontWithName:@"AvenirNextCondensed-DemiBold" size:14.0f]];
    [sectionHeaderView.textLabel setTextColor:[UIColor darkGrayColor]];

    NSLog(@"Section background: %@", sectionHeaderView.backgroundView);

    return sectionHeaderView;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();

    //  Bottom border
    //
    CGContextSetFillColorWithColor(context, [[UIColor greenColor] colorWithAlphaComponent:0.12f].CGColor);
    CGContextFillRect(context, CGRectMake(0.0f, CGRectGetHeight(self.contentView.frame) - 1.0f, CGRectGetWidth(self.contentView.frame), 1.0f));
}

它成功地记录了具有正确图像背景颜色和标题的背景视图。此外,textLabel 的颜色也会发生变化。但不是其他两个。

有谁知道为什么?

谢谢

4

0 回答 0