1

在我的 UICollectionViewCell 子类中,我有这段代码来布局我想要的子视图:

 - (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(97, 10, 190, 40)];
        _titleLabel.font = [UIFont boldFlatFontOfSize:22];
        _titleLabel.backgroundColor = [UIColor clearColor];

        _fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(97, 35, 190, 30)];
        _fromLabel.font = [UIFont flatFontOfSize:14];
        _fromLabel.backgroundColor = [UIColor clearColor];


        UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
        containerView.backgroundColor = [[UIColor colorWithPatternImage:[UIImage imageNamed:@"cream_pixels"]] colorWithAlphaComponent:0.8];
        [self.contentView addSubview:containerView];

        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
        [_iconView setCenter:containerView.center];
        _iconView.backgroundColor = [UIColor whiteColor];
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
        _iconView.image = [UIImage imageNamed:@"envelopeIcon.png"];
        _iconView.backgroundColor = [UIColor clearColor];
        [containerView addSubview:_iconView];

        _dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(97, self.contentView.frame.size.height - 23, self.contentView.frame.size.width-97, 25)];
        [_dateLabel setBackgroundColor:[UIColor clearColor]];
        [_dateLabel setFont:[UIFont boldFlatFontOfSize:13]];
        [_dateLabel setTextColor:[[UIColor wetAsphaltColor] colorWithAlphaComponent:0.6f]];
        [_dateLabel setTextAlignment:NSTextAlignmentLeft];
        [_dateLabel setLineBreakMode:NSLineBreakByCharWrapping];
        [_dateLabel setNumberOfLines:0];

        _typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.f, 0.f , 4, self.contentView.frame.size.height)];
        _typeLabel.layer.cornerRadius = 0.f;
        _typeLabel.backgroundColor = [UIColor belizeHoleColor];

        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_fromLabel];
        [self.contentView addSubview:_typeLabel];
        [self.contentView addSubview:_dateLabel];

        self.backgroundColor = [[UIColor cloudsColor] colorWithAlphaComponent:1.f];
        self.layer.cornerRadius = 3.2f;

        self.layer.masksToBounds = NO;
        self.layer.contentsScale = [UIScreen mainScreen].scale;
        self.layer.borderColor = [[UIColor silverColor] colorWithAlphaComponent:0.2].CGColor;
        self.layer.borderWidth = 0.5f;
        self.layer.shadowOpacity = 0.35f;
        self.layer.shadowRadius = 3.f;
        self.layer.shadowOffset = CGSizeMake(0, 3.f);
        self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
        self.layer.shouldRasterize = YES;

        [_titleLabel setText:@"Nuevo Mensaje"];
    }
    return self;
}

我知道为了提高性能,建议使用自定义绘图将子视图添加到单元格中,而不是通过对其内容进行分层。

我怎样才能做到这一点?

4

0 回答 0