RankHeadView.m 5 KB
Newer Older
1 2 3 4 5 6 7 8 9
//
//  RankHeadView.m
//  redstar
//
//  Created by admin on 15/11/12.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "RankHeadView.h"
admin's avatar
admin committed
10
#import "TaskGroup.h"
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

@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
{
admin's avatar
admin committed
74
    _taskGroup.opened = !_taskGroup.isOpened;
75 76 77 78 79
    if ([_delegate respondsToSelector:@selector(clickRankHeadView)]) {
        [_delegate clickRankHeadView];
    }
}

admin's avatar
admin committed
80
- (void)setTaskGroup:(TaskGroup *)taskGroup
81
{
admin's avatar
admin committed
82
    _taskGroup = taskGroup;
83
    
admin's avatar
admin committed
84
    [_bgButton setTitle:taskGroup.category forState:UIControlStateNormal];
85 86 87 88 89 90
    NSString *scoreStr = nil;
    if (taskGroup.score == NULL || taskGroup.score == nil) {
        scoreStr = @"0 分";
    } else {
        scoreStr = [NSString stringWithFormat:@"%@ 分", taskGroup.score];
    }
admin's avatar
admin committed
91
    
92 93 94 95 96
    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];
    
97 98 99 100 101 102
    NSString *rankStr = nil;
    if (taskGroup.score == NULL || taskGroup.score == nil) {
        rankStr = @"0 分";
    } else {
        rankStr = [NSString stringWithFormat:@"第 %@ 名", taskGroup.ranking];
    }
103 104 105 106 107 108 109 110 111 112 113 114
    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
{
admin's avatar
admin committed
115 116
    _arrowImageView.transform = _taskGroup.isOpened ? CGAffineTransformMakeRotation(M_PI) : CGAffineTransformMakeRotation(0);
    _lineView.hidden = _taskGroup.isOpened ? YES : NO;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}

- (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