| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- //
- // UIButton+CAGradientLayer.m
- // BuGuDY
- //
- // Created by bugu on 2020/3/14.
- // Copyright © 2020 宋晨光. All rights reserved.
- //
- #import "UIButton+CAGradientLayer.h"
- #import "UIFont+Ext.h"
- @implementation UIButton (CAGradientLayer)
- + (instancetype)buttonGradientFrame:(CGRect)frame Title:(NSString *)title target:(id)vc
- action:(SEL)action{
-
- return ({
- UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
-
- if (title) {
- [button setTitle:title forState:UIControlStateNormal];
- }
- button.frame = frame;
- [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
-
- button.titleLabel.font = [UIFont systemFontOfSize:16];
-
- if (action) {
- [button addTarget:vc action:action forControlEvents:UIControlEventTouchUpInside];
- }
- //非常重要 设置 button.layer.masksToBounds = YES时阴影 就不能实现。所以把masksToBounds设置在 渐变层
- // button.layer.masksToBounds = YES;
- // button.layer.cornerRadius = frame.size.height/2;
-
-
- // gradient
- CAGradientLayer *gl = [CAGradientLayer layer];
- gl.frame = button.bounds;
- gl.cornerRadius = frame.size.height/2;
- gl.masksToBounds = YES;
- gl.startPoint = CGPointMake(0, 0);
- gl.endPoint = CGPointMake(1, 0);
- gl.colors = @[(__bridge id)[UIColor colorWithHexString:@"#AE2CF1"].CGColor,(__bridge id)[UIColor colorWithHexString:@"#AE2CF1"].CGColor];
- gl.locations = @[@(0.0),@(1.0f)];
-
- [button.layer insertSublayer:gl atIndex:0];
- button;
- });
-
- }
- + (instancetype)buttonPinkLayerFrame:(CGRect)frame Title:(NSString *)title target:(id)vc
- action:(SEL)action{
-
-
-
- return ({
- UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
-
- if (title) {
- [button setTitle:title forState:UIControlStateNormal];
- }
- button.frame = frame;
- // button.backgroundColor = kRandomColor;
- [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
-
- button.titleLabel.font = UIFont.bg_mediumFont16;
-
-
- if (action) {
- [button addTarget:vc action:action forControlEvents:UIControlEventTouchUpInside];
- }
-
- button.layer.cornerRadius = frame.size.height/2;
-
- button.layer.borderColor = kWhiteColor.CGColor;
- button.layer.borderWidth = 1;
-
-
- button;
- });
-
-
- }
- + (instancetype)buttonLayerColor:(UIColor *)color Frame:(CGRect)frame Title:(NSString *)title target:(id)vc
- action:(SEL)action{
- return ({
- UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
-
- if (title) {
- [button setTitle:title forState:UIControlStateNormal];
- }
- button.frame = frame;
- // button.backgroundColor = kRandomColor;
- [button setTitleColor:color forState:UIControlStateNormal];
-
- button.titleLabel.font = UIFont.bg_mediumFont16;
-
-
- if (action) {
- [button addTarget:vc action:action forControlEvents:UIControlEventTouchUpInside];
- }
-
- button.layer.cornerRadius = frame.size.height/2;
-
- button.layer.borderColor = color.CGColor;
- button.layer.borderWidth = 1;
-
-
- button;
- });
-
-
- }
- @end
|