UIButton在项目中的应用

1.设置圆角

 [self.loginBtn.layer setMasksToBounds:YES];
 [self.loginBtn.layer setCornerRadius:5.0];

2.用代码设置图片与文字的间距

    UIButton *searchBar = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, TWScreenW - 10, 25)];
    [searchBar setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [searchBar setTitle:@"搜索" forState:UIControlStateNormal];
    searchBar.titleLabel.font = [UIFont systemFontOfSize:12];
    [searchBar setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
    [searchBar setBackgroundColor:[UIColor whiteColor]];
    [searchBar setTitleColor:TWTextColor forState:UIControlStateNormal];
    [searchBar addTarget:self action:@selector(searchBarClick) forControlEvents:UIControlEventTouchDown];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TWScreenW, 35)];
    view.backgroundColor = TWRGBColor(244, 244, 244);
    [view addSubview:searchBar];

3.自定义图片与文字上下排布的按钮

#import 

@interface TWVerticalButton : UIButton

@end
#import "TWVerticalButton.h"

@implementation TWVerticalButton

- (void)setup
{
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleLabel.font = [UIFont systemFontOfSize:9];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib
{
    [self setup];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 调整图片
    self.imageView.x = 6;
    self.imageView.y = 0;
    self.imageView.width = 14;
    self.imageView.height = 14;
    
    // 调整文字
    self.titleLabel.x = 0;
    self.titleLabel.y = self.imageView.height+4;
    self.titleLabel.width = self.width;
    self.titleLabel.height = self.height - self.titleLabel.y;
}


@end

4.自定义红色提醒按钮

#import 

@interface IWBadgeButton : UIButton
@property (nonatomic, copy) NSString *badgeValue;
@end
#import "IWBadgeButton.h"
#import "UIImage+MJ.h"
@implementation IWBadgeButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        self.userInteractionEnabled = NO;
        [self setBackgroundImage:[UIImage resizedImageWithName:@"main_badge_os7"] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:11];
    }
    return self;
}

- (void)setBadgeValue:(NSString *)badgeValue
{

    _badgeValue = [badgeValue copy];
    
    self.titleLabel.text = badgeValue;
    
    if (badgeValue && [badgeValue intValue] != 0) {
        self.hidden = NO;
        // 设置文字
        [self setTitle:badgeValue forState:UIControlStateNormal];
        
        // 设置frame
        CGRect frame = self.frame;
        CGFloat badgeH = self.currentBackgroundImage.size.height;
        CGFloat badgeW = self.currentBackgroundImage.size.width;
        if (badgeValue.length > 1) {
            // 文字的尺寸
//            CGSize badgeSize = [badgeValue sizeWithFont:self.titleLabel.font];
            
            badgeW = badgeW + 10;
        }
        frame.size.width = badgeW;
        frame.size.height = badgeH;
        self.frame = frame;
    } else {
        self.hidden = YES;
    }
}

5.自定义倒计时按钮

#import 

@interface CountButton : UIButton

@property (nonatomic,assign) NSInteger number;

//开始计数
- (void)startCountDown;

@end
#import "CountButton.h"

@implementation CountButton{
    NSTimer *_timer;
}
//代码初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setTitle:@"发送验证码" forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:11];
        [self setTitle:@"60s秒后重新发送" forState:UIControlStateSelected];
        [self setBackgroundColor:[UIColor colorWithRed:106 / 255.0 green:144 / 255.0 blue:227 / 255.0 alpha:1.0]];
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.number = 60;
    }
    return self;
}

//开始倒计时
- (void)startCountDown{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countSixty) userInfo:nil repeats:YES];
    self.enabled = NO;
    _number = 60;
    [self setTitle:@"60秒后重新发送" forState:UIControlStateNormal];
    //字体设小点
    self.titleLabel.font = [UIFont systemFontOfSize:12];
}

- (void)countSixty{
    //到0重置按钮
    if (_number == 0) {
        self.enabled = YES;
        _number = 60;
        [_timer invalidate];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self setTitle:@"发送验证码" forState:UIControlStateNormal];
        return;
    }
    _number --;
    NSString *string = [NSString stringWithFormat:@"%lds秒后重新发送",(long)_number];
    [self setTitle:string forState:UIControlStateNormal];
}

6.UIButton 怎么设置文字左对齐

// button.titleLabel.textAlignment = NSTextAlignmentLeft; 这句无效  
      button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;  
      button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);  

这里使用

button.titleLabel.textAlignment = NSTextAlignmentLeft; 

这行代码是没有效果的,这只是让标签中的文本左对齐,但并没有改变标签在按钮中的对齐方式。所以,我们首先要使用

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 

这行代码,把按钮的内容(控件)的对齐方式修改为水平左对齐,但是这们会紧紧靠着左边,不好看,所以我们还可以修改属性:

button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

这行代码可以让按钮的内容(控件)距离左边10个像素,这样就好看多了

你可能感兴趣的:(UIButton在项目中的应用)