UIButton+Corner.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // UIButton+Corner.m
  3. //
  4. // Created by dyf on 2018/01/28.
  5. // Copyright © 2018 dyf. All rights reserved.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import "UIButton+Corner.h"
  26. const VVBorder VVBorderNull = {0, nil};
  27. @implementation UIButton (Corner)
  28. - (ClipCornerBlock)clipCorner {
  29. ClipCornerBlock block = ^(UIRectCorner rc, UIColor *color, CGFloat radius, VVBorder border) {
  30. CGRect mRect = self.bounds;
  31. CGSize mSize = mRect.size;
  32. CGSize radii = CGSizeMake(radius, radius);
  33. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:mRect byRoundingCorners:rc cornerRadii:radii];
  34. UIImage *mImage = nil;
  35. if (@available(iOS 10.0, *)) {
  36. UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc] initWithSize:mSize];
  37. mImage = [render imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
  38. UIGraphicsImageRendererContext *ctx = rendererContext;
  39. CGContextSetFillColorWithColor (ctx.CGContext, color.CGColor);
  40. CGContextSetStrokeColorWithColor(ctx.CGContext, border.color.CGColor);
  41. CGContextSetLineWidth (ctx.CGContext, border.width);
  42. [path addClip];
  43. CGContextAddPath (ctx.CGContext, path.CGPath);
  44. CGContextDrawPath(ctx.CGContext, kCGPathFillStroke);
  45. }];
  46. } else {
  47. UIGraphicsBeginImageContext(mSize);
  48. CGContextRef context = UIGraphicsGetCurrentContext();
  49. CGContextSetFillColorWithColor (context, color.CGColor);
  50. CGContextSetStrokeColorWithColor(context, border.color.CGColor);
  51. CGContextSetLineWidth (context, border.width);
  52. [path addClip];
  53. CGContextAddPath (context, path.CGPath);
  54. CGContextDrawPath(context, kCGPathFillStroke);
  55. mImage = UIGraphicsGetImageFromCurrentImageContext();
  56. UIGraphicsEndImageContext();
  57. }
  58. [self setBackgroundImage:mImage forState:UIControlStateNormal];
  59. [self setBackgroundImage:mImage forState:UIControlStateHighlighted];
  60. };
  61. return block;
  62. }
  63. @end