开发中总是遇到创建个人信息和添加活动的页面,用tableview时取值判断感觉麻烦,手搭又感觉费劲,就偷懒自定义了下button,也算是简单的用法,不知道值不值得推荐,反正个人感觉在这里还是挺方便的,贴出来还望大家多多指教
.h
#import <UIKit/UIKit.h>
@interface AddActivityButton : UIButton
@property (copy, nonatomic)NSString *detail;
@end
.m
#import "AddActivityButton.h"
@interface AddActivityButton()
{
UILabel *_detailLabel;
}
@end
@implementation AddActivityButton
- (instancetype)init
{
self = [super init];
if (self) {
@throw [NSException exceptionWithName:@"AddActivityButton Exception" reason:@"must use method:initWithFrame:" userInfo:nil];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.titleLabel.textAlignment = NSTextAlignmentLeft;
self.titleLabel.textColor = [UIColor grayColor];
_detailLabel = [RMUtils labelWith:CGRectMake(self.width/2, 5, self.width/2 - 30, frame.size.height - 10) font:[RMUtils defaultFont] text:_detail textColor:[UIColor grayColor]];
_detailLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:_detailLabel];
}
return self;
}
- (void)setDetail:(NSString *)detail
{
_detailLabel.text = detail;
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat x = 5;
CGFloat y = 5;
CGFloat width = contentRect.size.width/2 - 10;
CGFloat height = contentRect.size.height - 10;
return CGRectMake(x, y, width, height);
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat x = self.width- 25;
CGFloat y = 15;
CGFloat width = 10;
CGFloat height = 10;
return CGRectMake(x, y, width, height);
}
创建后的button图
textfield,textview这些控件也有这些方法,都可以进行自定义,比如说想在textfield和textview的前面流出空格加个,改变frame了,都可以继承父类实现方法,