黑马程序员技术交流社区
标题:
给UIButton写的一个分类
[打印本页]
作者:
vitanie
时间:
2015-3-9 00:51
标题:
给UIButton写的一个分类
#import "UIButton+More.h"
@implementation UIButton (More)
//UIButton的title居左对齐
//btn.contentHorizontalAlignment = 1;
//使文字距离做边框保持10个像素的距离。
//btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
/**
设置文字左对齐并留出指定缩进
*/
- (void)setTextLeftEdgeInsets:(float)insets
{
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.contentEdgeInsets = UIEdgeInsetsMake(0, insets, 0, 0);
}
/**
* 1. 无图无字
*/
+ (UIButton *)ButtonWithFrame:(CGRect)rect target:(id)target action:(SEL)action backColor:(UIColor *)color
{
UIButton *button = [[UIButton alloc] initWithFrame:rect];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:color];
return button;
}
/**
* 2. 背景图
*/
+ (UIButton *)ButtonWithFrame:(CGRect)rect target:(id)target action:(SEL)action backImage:(UIImage *)image
{
UIButton *button = [[UIButton alloc] initWithFrame:rect];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor clearColor]];
button.clipsToBounds = YES;
return button;
}
/**
* 3. 文字
*/
+ (UIButton *)ButtonWithFrame:(CGRect)rect target:(id)target action:(SEL)action title:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font backColor:(UIColor *)color
{
UIButton *button = [[UIButton alloc] initWithFrame:rect];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:titleColor forState:UIControlStateNormal];
[button.titleLabel setFont:font];
[button setBackgroundColor:color];
button.clipsToBounds = YES;
return button;
}
/**
* 4. 背景图 + 文字
*/
+ (UIButton *)ButtonWithFrame:(CGRect)rect target:(id)target action:(SEL)action backImage:(UIImage *)image title:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font
{
UIButton *button = [[UIButton alloc] initWithFrame:rect];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:titleColor forState:UIControlStateNormal];
[button.titleLabel setFont:font];
[button setBackgroundColor:[UIColor clearColor]];
button.clipsToBounds = YES;
return button;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2