NSBezierPath+SDRoundedCorners.m 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "NSBezierPath+SDRoundedCorners.h"
  9. #if SD_MAC
  10. @implementation NSBezierPath (SDRoundedCorners)
  11. + (instancetype)sd_bezierPathWithRoundedRect:(NSRect)rect byRoundingCorners:(SDRectCorner)corners cornerRadius:(CGFloat)cornerRadius {
  12. NSBezierPath *path = [NSBezierPath bezierPath];
  13. CGFloat maxCorner = MIN(NSWidth(rect), NSHeight(rect)) / 2;
  14. CGFloat topLeftRadius = MIN(maxCorner, (corners & SDRectCornerTopLeft) ? cornerRadius : 0);
  15. CGFloat topRightRadius = MIN(maxCorner, (corners & SDRectCornerTopRight) ? cornerRadius : 0);
  16. CGFloat bottomLeftRadius = MIN(maxCorner, (corners & SDRectCornerBottomLeft) ? cornerRadius : 0);
  17. CGFloat bottomRightRadius = MIN(maxCorner, (corners & SDRectCornerBottomRight) ? cornerRadius : 0);
  18. NSPoint topLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect));
  19. NSPoint topRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
  20. NSPoint bottomLeft = NSMakePoint(NSMinX(rect), NSMinY(rect));
  21. NSPoint bottomRight = NSMakePoint(NSMaxX(rect), NSMinY(rect));
  22. [path moveToPoint:NSMakePoint(NSMidX(rect), NSMaxY(rect))];
  23. [path appendBezierPathWithArcFromPoint:topLeft toPoint:bottomLeft radius:topLeftRadius];
  24. [path appendBezierPathWithArcFromPoint:bottomLeft toPoint:bottomRight radius:bottomLeftRadius];
  25. [path appendBezierPathWithArcFromPoint:bottomRight toPoint:topRight radius:bottomRightRadius];
  26. [path appendBezierPathWithArcFromPoint:topRight toPoint:topLeft radius:topRightRadius];
  27. [path closePath];
  28. return path;
  29. }
  30. @end
  31. #endif