Skip to content
This repository was archived by the owner on Aug 31, 2019. It is now read-only.

Commit 1a6448b

Browse files
author
Reda Lemeden
committed
Implement drawRect()
1 parent 0d6ae5b commit 1a6448b

16 files changed

+283
-21
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pbxproj binary merge=union

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.DS_Store
2+
build/
3+
*.pbxuser
4+
*.mode1v3
5+
*.mode2v3
6+
*.perspectivev3
7+
profile
8+
xcuserdata
9+
*.swp
10+
*~.nib
11+
project.xcworkspace/

Custom UIButtons.xcodeproj/project.pbxproj

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
9D5AADB11618F5F90000EF26 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9D5AAD8C1618F5F90000EF26 /* Foundation.framework */; };
2424
9D5AADB91618F5F90000EF26 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 9D5AADB71618F5F90000EF26 /* InfoPlist.strings */; };
2525
9D5AADBC1618F5F90000EF26 /* Custom_UIButtonsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D5AADBB1618F5F90000EF26 /* Custom_UIButtonsTests.m */; };
26+
9D5AADCD161904190000EF26 /* button.png in Resources */ = {isa = PBXBuildFile; fileRef = 9D5AADC9161904190000EF26 /* button.png */; };
27+
9D5AADCE161904190000EF26 /* button@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 9D5AADCA161904190000EF26 /* button@2x.png */; };
28+
9D5AADCF161904190000EF26 /* buttonHighlighted.png in Resources */ = {isa = PBXBuildFile; fileRef = 9D5AADCB161904190000EF26 /* buttonHighlighted.png */; };
29+
9D5AADD0161904190000EF26 /* buttonHighlighted@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 9D5AADCC161904190000EF26 /* buttonHighlighted@2x.png */; };
30+
9D5AADD51619BB250000EF26 /* resizableButton.png in Resources */ = {isa = PBXBuildFile; fileRef = 9D5AADD11619BB250000EF26 /* resizableButton.png */; };
31+
9D5AADD61619BB250000EF26 /* resizableButton@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 9D5AADD21619BB250000EF26 /* resizableButton@2x.png */; };
32+
9D5AADD71619BB250000EF26 /* resizableButtonHighlighted.png in Resources */ = {isa = PBXBuildFile; fileRef = 9D5AADD31619BB250000EF26 /* resizableButtonHighlighted.png */; };
33+
9D5AADD81619BB250000EF26 /* resizableButtonHighlighted@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 9D5AADD41619BB250000EF26 /* resizableButtonHighlighted@2x.png */; };
34+
9DDA94DF161F4B0F0099D9C3 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9DDA94DE161F4B0F0099D9C3 /* QuartzCore.framework */; };
35+
9DDA94F5161F68C90099D9C3 /* CBLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DDA94F4161F68C90099D9C3 /* CBLayer.m */; };
2636
/* End PBXBuildFile section */
2737

2838
/* Begin PBXContainerItemProxy section */
@@ -58,13 +68,25 @@
5868
9D5AADB81618F5F90000EF26 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
5969
9D5AADBA1618F5F90000EF26 /* Custom_UIButtonsTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Custom_UIButtonsTests.h; sourceTree = "<group>"; };
6070
9D5AADBB1618F5F90000EF26 /* Custom_UIButtonsTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Custom_UIButtonsTests.m; sourceTree = "<group>"; };
71+
9D5AADC9161904190000EF26 /* button.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = button.png; sourceTree = "<group>"; };
72+
9D5AADCA161904190000EF26 /* button@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "button@2x.png"; sourceTree = "<group>"; };
73+
9D5AADCB161904190000EF26 /* buttonHighlighted.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = buttonHighlighted.png; sourceTree = "<group>"; };
74+
9D5AADCC161904190000EF26 /* buttonHighlighted@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "buttonHighlighted@2x.png"; sourceTree = "<group>"; };
75+
9D5AADD11619BB250000EF26 /* resizableButton.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = resizableButton.png; sourceTree = "<group>"; };
76+
9D5AADD21619BB250000EF26 /* resizableButton@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "resizableButton@2x.png"; sourceTree = "<group>"; };
77+
9D5AADD31619BB250000EF26 /* resizableButtonHighlighted.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = resizableButtonHighlighted.png; sourceTree = "<group>"; };
78+
9D5AADD41619BB250000EF26 /* resizableButtonHighlighted@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "resizableButtonHighlighted@2x.png"; sourceTree = "<group>"; };
79+
9DDA94DE161F4B0F0099D9C3 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
80+
9DDA94F3161F68C90099D9C3 /* CBLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CBLayer.h; sourceTree = "<group>"; };
81+
9DDA94F4161F68C90099D9C3 /* CBLayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CBLayer.m; sourceTree = "<group>"; };
6182
/* End PBXFileReference section */
6283

6384
/* Begin PBXFrameworksBuildPhase section */
6485
9D5AAD831618F5F90000EF26 /* Frameworks */ = {
6586
isa = PBXFrameworksBuildPhase;
6687
buildActionMask = 2147483647;
6788
files = (
89+
9DDA94DF161F4B0F0099D9C3 /* QuartzCore.framework in Frameworks */,
6890
9D5AAD8B1618F5F90000EF26 /* UIKit.framework in Frameworks */,
6991
9D5AAD8D1618F5F90000EF26 /* Foundation.framework in Frameworks */,
7092
9D5AAD8F1618F5F90000EF26 /* CoreGraphics.framework in Frameworks */,
@@ -106,6 +128,7 @@
106128
9D5AAD891618F5F90000EF26 /* Frameworks */ = {
107129
isa = PBXGroup;
108130
children = (
131+
9DDA94DE161F4B0F0099D9C3 /* QuartzCore.framework */,
109132
9D5AAD8A1618F5F90000EF26 /* UIKit.framework */,
110133
9D5AAD8C1618F5F90000EF26 /* Foundation.framework */,
111134
9D5AAD8E1618F5F90000EF26 /* CoreGraphics.framework */,
@@ -120,6 +143,8 @@
120143
9D5AAD991618F5F90000EF26 /* CBAppDelegate.h */,
121144
9D5AAD9A1618F5F90000EF26 /* CBAppDelegate.m */,
122145
9D5AADA21618F5F90000EF26 /* MainStoryboard.storyboard */,
146+
9DDA94F3161F68C90099D9C3 /* CBLayer.h */,
147+
9DDA94F4161F68C90099D9C3 /* CBLayer.m */,
123148
9D5AADA51618F5F90000EF26 /* CBViewController.h */,
124149
9D5AADA61618F5F90000EF26 /* CBViewController.m */,
125150
9D5AAD911618F5F90000EF26 /* Supporting Files */,
@@ -130,6 +155,14 @@
130155
9D5AAD911618F5F90000EF26 /* Supporting Files */ = {
131156
isa = PBXGroup;
132157
children = (
158+
9D5AADD11619BB250000EF26 /* resizableButton.png */,
159+
9D5AADD21619BB250000EF26 /* resizableButton@2x.png */,
160+
9D5AADD31619BB250000EF26 /* resizableButtonHighlighted.png */,
161+
9D5AADD41619BB250000EF26 /* resizableButtonHighlighted@2x.png */,
162+
9D5AADC9161904190000EF26 /* button.png */,
163+
9D5AADCA161904190000EF26 /* button@2x.png */,
164+
9D5AADCB161904190000EF26 /* buttonHighlighted.png */,
165+
9D5AADCC161904190000EF26 /* buttonHighlighted@2x.png */,
133166
9D5AAD921618F5F90000EF26 /* Custom UIButtons-Info.plist */,
134167
9D5AAD931618F5F90000EF26 /* InfoPlist.strings */,
135168
9D5AAD961618F5F90000EF26 /* main.m */,
@@ -237,6 +270,14 @@
237270
9D5AAD9F1618F5F90000EF26 /* Default@2x.png in Resources */,
238271
9D5AADA11618F5F90000EF26 /* Default-568h@2x.png in Resources */,
239272
9D5AADA41618F5F90000EF26 /* MainStoryboard.storyboard in Resources */,
273+
9D5AADCD161904190000EF26 /* button.png in Resources */,
274+
9D5AADCE161904190000EF26 /* button@2x.png in Resources */,
275+
9D5AADCF161904190000EF26 /* buttonHighlighted.png in Resources */,
276+
9D5AADD0161904190000EF26 /* buttonHighlighted@2x.png in Resources */,
277+
9D5AADD51619BB250000EF26 /* resizableButton.png in Resources */,
278+
9D5AADD61619BB250000EF26 /* resizableButton@2x.png in Resources */,
279+
9D5AADD71619BB250000EF26 /* resizableButtonHighlighted.png in Resources */,
280+
9D5AADD81619BB250000EF26 /* resizableButtonHighlighted@2x.png in Resources */,
240281
);
241282
runOnlyForDeploymentPostprocessing = 0;
242283
};
@@ -274,6 +315,7 @@
274315
9D5AAD971618F5F90000EF26 /* main.m in Sources */,
275316
9D5AAD9B1618F5F90000EF26 /* CBAppDelegate.m in Sources */,
276317
9D5AADA71618F5F90000EF26 /* CBViewController.m in Sources */,
318+
9DDA94F5161F68C90099D9C3 /* CBLayer.m in Sources */,
277319
);
278320
runOnlyForDeploymentPostprocessing = 0;
279321
};
@@ -447,6 +489,7 @@
447489
9D5AADC11618F5F90000EF26 /* Release */,
448490
);
449491
defaultConfigurationIsVisible = 0;
492+
defaultConfigurationName = Release;
450493
};
451494
9D5AADC21618F5F90000EF26 /* Build configuration list for PBXNativeTarget "Custom UIButtonsTests" */ = {
452495
isa = XCConfigurationList;
@@ -455,6 +498,7 @@
455498
9D5AADC41618F5F90000EF26 /* Release */,
456499
);
457500
defaultConfigurationIsVisible = 0;
501+
defaultConfigurationName = Release;
458502
};
459503
/* End XCConfigurationList section */
460504
};

Custom UIButtons/CBLayer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#import <UIKit/UIKit.h>
2+
#import <QuartzCore/QuartzCore.h>
3+
4+
@interface CBLayer : UIButton
5+
6+
@end

Custom UIButtons/CBLayer.m

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#import "CBLayer.h"
2+
3+
@interface CBLayer ()
4+
5+
@end
6+
7+
@implementation CBLayer
8+
9+
#pragma mark -
10+
- (void)awakeFromNib
11+
{
12+
13+
}
14+
15+
- (void)drawRect:(CGRect)rect
16+
{
17+
[self drawBorder];
18+
[self drawInnerGlow];
19+
[self drawBackgroundGradient];
20+
}
21+
22+
- (void)drawBackgroundGradient
23+
{
24+
// Instantiate the gradient layer
25+
CAGradientLayer *gradient = [CAGradientLayer layer];
26+
27+
// Set the gradient frame (make the gradient fill the whole button)
28+
gradient.frame = self.bounds;
29+
30+
31+
// Set the colors
32+
gradient.colors = (@[
33+
(id)[UIColor colorWithRed:0.94f green:0.82f blue:0.52f alpha:1.00f].CGColor,
34+
(id)[UIColor colorWithRed:0.91f green:0.58f blue:0.00f alpha:1.00f].CGColor
35+
]);
36+
37+
// Set the stops
38+
gradient.locations = (@[
39+
@0.0f,
40+
@1.0f
41+
]);
42+
43+
// Add the gradient to the layer hierarchy
44+
[self.layer insertSublayer:gradient atIndex:0];
45+
}
46+
47+
- (void)drawBackgroundGradientHighlighted
48+
{
49+
// Instantiate the gradient layer
50+
CAGradientLayer *gradient = [CAGradientLayer layer];
51+
52+
// Set the gradient frame (make the gradient fill the whole button)
53+
gradient.frame = self.bounds;
54+
55+
// Set the colors
56+
gradient.colors = (@[
57+
(id)[UIColor colorWithRed:0.3f green:0.82f blue:0.52f alpha:1.00f].CGColor,
58+
(id)[UIColor colorWithRed:0.91f green:0.58f blue:0.00f alpha:1.00f].CGColor
59+
]);
60+
61+
// Set the stops
62+
gradient.locations = (@[
63+
@0.0f,
64+
@1.0f
65+
]);
66+
//gradient.hidden = Yes;
67+
68+
// Add the gradient to the layer hierarchy
69+
[self.layer insertSublayer:gradient atIndex:0];
70+
}
71+
72+
- (void)drawInnerGlow
73+
{
74+
CALayer *innerglow = [CALayer layer];
75+
CGRect innerGlowFrame = CGRectMake(self.bounds.origin.x+1, self.bounds.origin.y+1, self.bounds.size.width-2, self.bounds.size.height-2);
76+
innerglow.frame = innerGlowFrame;
77+
innerglow.cornerRadius= 4.5f;
78+
innerglow.borderWidth = 1;
79+
innerglow.borderColor = [[UIColor whiteColor] CGColor];
80+
innerglow.opacity = 0.5;
81+
82+
[self.layer insertSublayer:innerglow atIndex:1];
83+
}
84+
85+
- (void)drawBorder
86+
{
87+
self.layer.cornerRadius = 4.5f;
88+
self.layer.masksToBounds = YES;
89+
self.layer.borderWidth = 1;
90+
self.layer.borderColor = [UIColor colorWithRed:0.77f green:0.43f blue:0.00f alpha:1.00f].CGColor;
91+
}
92+
93+
@end

Custom UIButtons/CBViewController.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
//
2-
// CBViewController.h
3-
// Custom UIButtons
4-
//
5-
// Created by Reda Lemeden on 9/30/12.
6-
// Copyright (c) 2012 thoughtbot. All rights reserved.
7-
//
8-
91
#import <UIKit/UIKit.h>
102

113
@interface CBViewController : UIViewController

Custom UIButtons/CBViewController.m

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
//
2-
// CBViewController.m
3-
// Custom UIButtons
4-
//
5-
// Created by Reda Lemeden on 9/30/12.
6-
// Copyright (c) 2012 thoughtbot. All rights reserved.
7-
//
8-
91
#import "CBViewController.h"
102

113
@interface CBViewController ()
124

5+
@property (weak, nonatomic) IBOutlet UIButton *fullSizeImage;
6+
@property (weak, nonatomic) IBOutlet UIButton *resizableImage;
7+
138
@end
149

1510
@implementation CBViewController
1611

1712
- (void)viewDidLoad
1813
{
1914
[super viewDidLoad];
20-
// Do any additional setup after loading the view, typically from a nib.
15+
16+
// Set full-sized background image
17+
[self.fullSizeImage setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
18+
[self.fullSizeImage setBackgroundImage:[UIImage imageNamed:@"buttonHighlighted.png"] forState:UIControlStateHighlighted];
19+
20+
// Define resizable images
21+
UIImage *resizableButton = [[UIImage imageNamed:@"resizableButton.png" ] resizableImageWithCapInsets:UIEdgeInsetsMake(17, 5, 17, 5)];
22+
UIImage *resizableButtonHighlighted = [[UIImage imageNamed:@"resizableButtonHighlighted.png" ] resizableImageWithCapInsets:UIEdgeInsetsMake(17, 5, 17, 5)];
23+
24+
// Set resizable background image
25+
[self.resizableImage setBackgroundImage:resizableButton forState:UIControlStateNormal];
26+
[self.resizableImage setBackgroundImage:resizableButtonHighlighted forState:UIControlStateHighlighted];
2127
}
2228

2329
- (void)didReceiveMemoryWarning

Custom UIButtons/button.png

1 KB
Loading

Custom UIButtons/button@2x.png

2.08 KB
Loading
1020 Bytes
Loading

0 commit comments

Comments
 (0)