// UEButton.h
// UIButtonInner
//
// Created by cdj on 17/9/9.
// Copyright © 2017年 ue. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UEButton : UIButton
@end
// UEButton.m
// UIButtonInner
//
// Created by cdj on 17/9/9.
// Copyright © 2017年 ue. All rights reserved.
//
#import "UEButton.h"
@implementation UEButton
/*
重写BUttion类的两个方法,改变图片和文字的位置
*/
//方式一
//-(CGRect)titleRectForContentRect:(CGRect)contentRect{
// return CGRectMake(0, 0, 100, 70);
//}
//
//-(CGRect)imageRectForContentRect:(CGRect)contentRect{
//
// return CGRectMake(100, 0, 70, 70);
//}
//方式二
-(void)layoutSubviews{
[super layoutSubviews];
self.titleLabel.frame = CGRectMake(0, 0, 100, 70);
self.imageView.frame = CGRectMake(100, 0, 70, 70);
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
//按钮文字剧中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
//改变图片的内容模式
self.imageView.contentMode = UIViewContentModeCenter;
}
return self;
}
@end
// ViewController.m
// UIButtonInner
//
// Created by cdj on 17/9/9.
// Copyright © 2017年 ue. All rights reserved.
//
#import "ViewController.h"
#import "UEButton.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UEButton *button = [UEButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 50, 170, 70);
button.backgroundColor = [UIColor purpleColor];
//添加图标:默认图标在左,文字在右
[button setTitle:@"normal" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"miniplayer_btn_playlist_normal"] forState:UIControlStateNormal];
//该变位置
button.imageView.backgroundColor = [UIColor yellowColor];
button.titleLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
}
@end