UI for iOS(四) -- UIButton添标题文字

by SevenJohs.

复选框设计

//  QWCheckBox.m
//  QWPlanInfo
//
//  Created by SevenJohs on 16/4/22.
//  Copyright © 2016年 ecity. All rights reserved.
//

#import "QWCheckBox.h"
#import <UIKit/UIKit.h>
@implementation QWCheckBox

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//重写父类方法
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.titleLabel.textAlignment = NSTextAlignmentRight;
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        self.titleLabel.textColor = [UIColor redColor];
        self.imageView.contentMode = UIViewContentModeLeft;
        [self setBackgroundImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];
        [self setBackgroundImage:[UIImage imageNamed:@"checkbox_selected"] forState:UIControlStateSelected];
    }
    return self;
}

-(CGRect)titleRectForContentRect:(CGRect)contentRect{

    CGFloat posW = self.frame.size.width - self.frame.size.height - 4 ;
    CGFloat posH = self.frame.size.height ;
    CGFloat posX = 0 ;
    CGFloat posY = 0 ;
    contentRect = CGRectMake(posX, posY, posW, posH);

    return contentRect;
}
-(CGRect)imageRectForContentRect:(CGRect)contentRect{

    CGFloat posW = self.frame.size.height - 14 ;
    CGFloat posH = self.frame.size.height - 14 ;
    CGFloat posX = self.frame.size.width - posW - 25 ;
    CGFloat posY = 0 ;
    contentRect = CGRectMake(posX, posY, posW, posH);

    return contentRect;
}

cell中实现代理方法

@protocol PlanInfoDelegate ;
@interface QWPlanInfoCell : UITableViewCell
//设置cell中的点击操作、值变化委托
@property (nonatomic,assign)id<PlanInfoDelegate> delegate;
@property (nonatomic, retain) QWCheckBox* checkBoxBtn;
@end

@protocol PlanInfoDelegate <NSObject>
-(void)planInfoCell:(QWPlanInfoCell*)cell stateChageForInfo:(QWPlanInfo *)data;
@end


/**
 *  根据父视图的大小设置CheckBox的frame,并传入数据
 *  @param rect 父视图frame
 *  @param data 数据
 */
-(void)updateCheckBoxSubviewsFrame:(CGRect)rect data:(QWPlanInfo *)data{

    CGFloat width = 30;
    CGFloat height = 12;
    CGFloat startX = (rect.size.width - EGDE_MARGIN*2)/2.5 * 2 ;
    CGFloat startY = (rect.size.height - height)/2;

    self.checkBoxBtn.frame = CGRectMake(startX, startY,width, height);
    self.checkBoxBtn.titleLabel.text = @"显示";
    [self.checkBoxBtn addTarget:self action:@selector(checkBoxClickAction) forControlEvents:UIControlEventTouchUpInside];

}


/**
 *  点击checkBox触发事件
 */
-(void)checkBoxClickAction{
    if(self.delegate){
        [self.delegate planInfoCell:self stateChageForInfo:_data];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值