-
Notifications
You must be signed in to change notification settings - Fork 17
/
NSBezierPath-RoundedRect.m
executable file
·43 lines (37 loc) · 1.78 KB
/
NSBezierPath-RoundedRect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#import "NSBezierPath-RoundedRect.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation NSBezierPath (RoundedRect)
///////////////////////////////////////////////////////////////////////////////////////////////////
+ (NSBezierPath *)bezierPathWithRoundedRect:(NSRect)rect cornerRadius:(float)radius
{
NSBezierPath *result = [NSBezierPath bezierPath];
[result appendBezierPathWithRoundedRect:rect cornerRadius:radius];
return result;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)appendBezierPathWithRoundedRect:(NSRect)rect cornerRadius:(float)radius
{
if (!NSIsEmptyRect(rect))
{
if (radius > 0.0)
{
float clampedRadius = MIN(radius, 0.5 * MIN(rect.size.width, rect.size.height));
NSPoint topLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect));
NSPoint topRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
NSPoint bottomRight = NSMakePoint(NSMaxX(rect), NSMinY(rect));
[self moveToPoint:NSMakePoint(NSMidX(rect), NSMaxY(rect))];
[self appendBezierPathWithArcFromPoint:topLeft toPoint:rect.origin radius:clampedRadius];
[self appendBezierPathWithArcFromPoint:rect.origin toPoint:bottomRight radius:clampedRadius];
[self appendBezierPathWithArcFromPoint:bottomRight toPoint:topRight radius:clampedRadius];
[self appendBezierPathWithArcFromPoint:topRight toPoint:topLeft radius:clampedRadius];
[self closePath];
}
else
{
[self appendBezierPathWithRect:rect];
}
}
}
@end