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

来源:互联网 发布:哈尔滨大麦网络 编辑:程序博客网 时间:2024/06/05 21:54

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];    }}
0 0
原创粉丝点击