UIButton+CAGradientLayer.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // UIButton+CAGradientLayer.m
  3. // BuGuDY
  4. //
  5. // Created by bugu on 2020/3/14.
  6. // Copyright © 2020 宋晨光. All rights reserved.
  7. //
  8. #import "UIButton+CAGradientLayer.h"
  9. #import "UIFont+Ext.h"
  10. @implementation UIButton (CAGradientLayer)
  11. + (instancetype)buttonGradientFrame:(CGRect)frame Title:(NSString *)title target:(id)vc
  12. action:(SEL)action{
  13. return ({
  14. UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  15. if (title) {
  16. [button setTitle:title forState:UIControlStateNormal];
  17. }
  18. button.frame = frame;
  19. [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
  20. button.titleLabel.font = [UIFont systemFontOfSize:16];
  21. if (action) {
  22. [button addTarget:vc action:action forControlEvents:UIControlEventTouchUpInside];
  23. }
  24. //非常重要 设置 button.layer.masksToBounds = YES时阴影 就不能实现。所以把masksToBounds设置在 渐变层
  25. // button.layer.masksToBounds = YES;
  26. // button.layer.cornerRadius = frame.size.height/2;
  27. // gradient
  28. CAGradientLayer *gl = [CAGradientLayer layer];
  29. gl.frame = button.bounds;
  30. gl.cornerRadius = frame.size.height/2;
  31. gl.masksToBounds = YES;
  32. gl.startPoint = CGPointMake(0, 0);
  33. gl.endPoint = CGPointMake(1, 0);
  34. gl.colors = @[(__bridge id)[UIColor colorWithHexString:@"#AE2CF1"].CGColor,(__bridge id)[UIColor colorWithHexString:@"#AE2CF1"].CGColor];
  35. gl.locations = @[@(0.0),@(1.0f)];
  36. [button.layer insertSublayer:gl atIndex:0];
  37. button;
  38. });
  39. }
  40. + (instancetype)buttonPinkLayerFrame:(CGRect)frame Title:(NSString *)title target:(id)vc
  41. action:(SEL)action{
  42. return ({
  43. UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  44. if (title) {
  45. [button setTitle:title forState:UIControlStateNormal];
  46. }
  47. button.frame = frame;
  48. // button.backgroundColor = kRandomColor;
  49. [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
  50. button.titleLabel.font = UIFont.bg_mediumFont16;
  51. if (action) {
  52. [button addTarget:vc action:action forControlEvents:UIControlEventTouchUpInside];
  53. }
  54. button.layer.cornerRadius = frame.size.height/2;
  55. button.layer.borderColor = kWhiteColor.CGColor;
  56. button.layer.borderWidth = 1;
  57. button;
  58. });
  59. }
  60. + (instancetype)buttonLayerColor:(UIColor *)color Frame:(CGRect)frame Title:(NSString *)title target:(id)vc
  61. action:(SEL)action{
  62. return ({
  63. UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  64. if (title) {
  65. [button setTitle:title forState:UIControlStateNormal];
  66. }
  67. button.frame = frame;
  68. // button.backgroundColor = kRandomColor;
  69. [button setTitleColor:color forState:UIControlStateNormal];
  70. button.titleLabel.font = UIFont.bg_mediumFont16;
  71. if (action) {
  72. [button addTarget:vc action:action forControlEvents:UIControlEventTouchUpInside];
  73. }
  74. button.layer.cornerRadius = frame.size.height/2;
  75. button.layer.borderColor = color.CGColor;
  76. button.layer.borderWidth = 1;
  77. button;
  78. });
  79. }
  80. @end