// MyweiboToolBarView.h
// 新浪微博
//
// Created by Jose on 15-4-21.
// Copyright (c) 2015年 jose. All rights reserved.
//
#import <UIKit/UIKit.h>
@class MyWeiboModel;
@interface MyweiboToolBarView : UIImageView
@property(nonatomic,assign)MyWeiboModel *weibo;
@end
*************************************************************************************************************
************************************************************************************************************
************************************************************************************************************
//
// MyweiboToolBarView.m
// 新浪微博
//
// Created by Jose on 15-4-21.
// Copyright (c) 2015年 jose. All rights reserved.
// 微博工具栏
#import "MyweiboToolBarView.h"
#import "UIImage+Extension.h"
#import "UIView+Extension.h"
#import "MyWeiboModel.h"
@interface MyweiboToolBarView()
/** 用来保存竖线 */
@property(nonatomic,strong)NSMutableArray *dividers;
/** 用来保存按钮 */
@property(nonatomic,strong)NSMutableArray *btns;
/** 评论按钮 */
@property(nonatomic,strong)UIButton *commentbtn;
/** 转发按钮 */
@property(nonatomic,strong)UIButton *repostbtn;
/** 赞按钮 */
@property(nonatomic,strong)UIButton *attitudebtn;
@end
@implementation MyweiboToolBarView
#pragma mark 懒加载模式
-(NSMutableArray *)dividers{
if (_dividers==nil) {
_dividers=[NSMutableArray array];
}
return _dividers;
}
-(NSMutableArray *)btns{
if (_btns==nil) {
_btns=[NSMutableArray array];
}
return _btns;
}
//初始化子控件
-(id)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
//设置可交互事件
self.userInteractionEnabled=YES;
self.image=[UIImage resizedimg:@"timeline_card_bottom_background"];
//添加按钮
_commentbtn=[self SetUpButtonWithIcon:@"timeline_icon_comment" Title:@"评论"];
_repostbtn=[self SetUpButtonWithIcon:@"timeline_icon_retweet" Title:@"转发"];
_attitudebtn=[self SetUpButtonWithIcon:@"timeline_icon_unlike" Title:@"赞"];
//添加竖线
[self SetUpDivider];
[self SetUpDivider];
}
return self;
}
//设置按钮
-(UIButton *)SetUpButtonWithIcon:(NSString *)icon Title:(NSString *)title{
UIButton *btn=[[UIButton alloc]init];
[btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
btn.titleLabel.font=[UIFont systemFontOfSize:14];
btn.titleEdgeInsets=UIEdgeInsetsMake(0, 10, 0, 0);
[btn setBackgroundImage:[UIImage imageNamed:@"common_card_bottom_background_highlighted"] forState:UIControlStateHighlighted];
btn.adjustsImageWhenDisabled=NO;
[self addSubview:btn];
[self.btns addObject:btn];
return btn;
}
//设置竖线
-(void)SetUpDivider{
UIImageView *divider=[[UIImageView alloc]init];
divider.image=[UIImage imageNamed:@"timeline_card_bottom_line"];
divider.contentMode=UIViewContentModeCenter;
[self addSubview:divider];
//注意self的使用
[self.dividers addObject:divider];
}
//布局子控件
-(void)layoutSubviews{
[super layoutSubviews];
int btncounts=(int)_btns.count;
CGFloat btnw=self.width/btncounts;
CGFloat btnh=self.height;
for (int i=0; i<btncounts; i++) {
UIButton *btn=_btns[i];
btn.width=btnw;
btn.height=btnh;
btn.y=0;
btn.x=i*btnw;
}
int dividercount=(int)_dividers.count;
for (int i=0; i<dividercount; i++) {
UIImageView *divider=_dividers[i];
divider.width=2;
divider.height=btnh;
divider.x=(1+i)*btnw;
divider.y=0;
}
}
//重写set方法,传入微博数据
-(void)setWeibo:(MyWeiboModel *)weibo{
_weibo=weibo;
[self SetUpButtonTitle:_commentbtn Count:weibo.comments_count DefaultTitle:@"评论"];
[self SetUpButtonTitle:_repostbtn Count:weibo.reposts_count DefaultTitle:@"转发"];
[self SetUpButtonTitle:_attitudebtn Count:weibo.attitudes_count DefaultTitle:@"赞"];
}
//设置标题
-(void)SetUpButtonTitle:(UIButton *)button Count:(int)count DefaultTitle:(NSString *)defaulttitle{
if (count>=10000) {
defaulttitle=[NSString stringWithFormat:@"%.1f万",count/10000.0];
//用空字符串替换所有的.0
defaulttitle=[defaulttitle stringByReplacingOccurrencesOfString:@".0" withString:@""];
}
else if (count>0){
defaulttitle=[NSString stringWithFormat:@"%d",count];
}
[button setTitle:defaulttitle forState:UIControlStateNormal];
}
@end