RankHeadView.m 4.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
//
//  RankHeadView.m
//  redstar
//
//  Created by admin on 15/11/12.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "RankHeadView.h"
#import "WorkModel.h"

@interface RankHeadView ()
{
    UIButton *_bgButton;
    UIImageView *_arrowImageView;
    UILabel *_scoreLabel; // 分数
    UILabel *_gradeLabel; // 排名
    UIView *_lineView;
}
@end

@implementation RankHeadView

+ (instancetype)headViewWithTableView:(UITableView *)tableView
{
    static NSString *headIdentifier = @"header";
    
    RankHeadView *headView = (RankHeadView *)[tableView dequeueReusableCellWithIdentifier:headIdentifier];
    if (headView == nil) {
        headView = [[RankHeadView alloc] initWithReuseIdentifier:headIdentifier];
    }
    
    return headView;
}

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
        bgButton.backgroundColor = [UIColor whiteColor];
        bgButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        bgButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
        bgButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        bgButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
        [bgButton setTitleColor:kAnnounceTextColor forState:UIControlStateNormal];
        [bgButton addTarget:self action:@selector(headBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:bgButton];
        _bgButton = bgButton;
        
        UIImageView *arrowIMView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_down"]];
        [self addSubview:arrowIMView];
        _arrowImageView = arrowIMView;
        
        UILabel *scoreLabel = [[UILabel alloc] init];
        scoreLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:scoreLabel];
        _scoreLabel = scoreLabel;
        
        UILabel *gradeLabel = [[UILabel alloc] init];
        gradeLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:gradeLabel];
        _gradeLabel = gradeLabel;
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = kSeparateLineColor;
        [self addSubview:lineView];
        _lineView = lineView;
    }
    return self;
}

- (void)headBtnClick
{
    _work.opened = !_work.isOpened;
    if ([_delegate respondsToSelector:@selector(clickRankHeadView)]) {
        [_delegate clickRankHeadView];
    }
}

- (void)setWork:(WorkModel *)work
{
    _work = work;
    [_bgButton setTitle:work.name forState:UIControlStateNormal];
    
    NSString *scoreStr = [NSString stringWithFormat:@"%@ 分", work.score];
    NSMutableAttributedString *scoreAttr = [[NSMutableAttributedString alloc] initWithString:scoreStr];
    [scoreAttr addAttributes:@{NSForegroundColorAttributeName:kNavigationBarColor,NSFontAttributeName:[UIFont systemFontOfSize:19.0f]} range:NSMakeRange(0,scoreAttr.length - 1)];
    [scoreAttr addAttributes:@{NSForegroundColorAttributeName:kDetailSmallTitleColor, NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} range:NSMakeRange(scoreAttr.length - 1,1)];
    [_scoreLabel setAttributedText:scoreAttr];
    
    NSString *rankStr = [NSString stringWithFormat:@"第 %@ 名", work.grade];
    NSMutableAttributedString *rankAttr = [[NSMutableAttributedString alloc] initWithString:rankStr];
    [rankAttr addAttributes:@{NSForegroundColorAttributeName:kRankHeadTitleTextColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} range:NSMakeRange(0,1)];
    [rankAttr addAttributes:@{NSForegroundColorAttributeName:kGradeNumberTextColor} range:NSMakeRange(1,rankStr.length - 2)];
    [rankAttr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18.0] range:NSMakeRange(1,rankStr.length - 2)];
    [rankAttr addAttributes:@{NSForegroundColorAttributeName:kRankHeadTitleTextColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} range:NSMakeRange(rankStr.length - 1,1)];
    [_gradeLabel setAttributedText:rankAttr];
}



- (void)didMoveToSuperview
{
    _arrowImageView.transform = _work.isOpened ? CGAffineTransformMakeRotation(M_PI) : CGAffineTransformMakeRotation(0);
    _lineView.hidden = _work.isOpened ? YES : NO;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _bgButton.frame = self.bounds;
    _scoreLabel.frame = CGRectMake(self.frame.size.width - 193, 0, 80, self.frame.size.height);
    _gradeLabel.frame = CGRectMake(self.frame.size.width - 113, 0, 80, self.frame.size.height);
    _arrowImageView.frame = CGRectMake(self.frame.size.width - 33, (self.frame.size.height - 8) / 2, 13, 8);
    _lineView.frame = CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1);
}


@end