新浪微博开发之三十四(微博工具栏view)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值