A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© vitanie 中级黑马   /  2015-3-9 00:51  /  883 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

#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;
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马