Graphics Reference
In-Depth Information
Another useful pair of
CALayer
properties are
borderWidth
and
borderColor
.
Together these define a line that is drawn around the edge of the layer. This line (known as
a
stroke
) follows the
bounds
of the layer, including the corner curvature.
The
borderWidth
is a floating-point number that defines the stroke thickness in points.
This defaults to zero (no border). The
borderColor
defines the color of the stroke and
defaults to black.
The type of
borderColor
is
CGColorRef
, not
UIColor
, so it's not a Cocoa object
per-se. However, you should be aware that the layer retains the
borderColor
, even
though there's no indication of this from the property declaration.
CGColorRef
behaves
like an
NSObject
in terms of retain/release, but the Objective-C syntax does not provide a
way to indicate this, so even strongly retained
CGColorRef
properties must be declared
using
assign
.
The border is drawn
inside
the layer bounds, and in front of any other layer contents,
including sublayers. If we modify the example to include a layer border (see Listing 4.2),
you can see how this works (see Figure 4.3).
Listing 4.2
Applying a Border
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//set the corner radius on our layers
self
.
layerView1
.
layer
.
cornerRadius
=
20.0f
;
self
.
layerView2
.
layer
.
cornerRadius
=
20.0f
;
//add a border to our layers
self
.
layerView1
.
layer
.
borderWidth
=
5.0f
;
self
.
layerView2
.
layer
.
borderWidth
=
5.0f
;
//enable clipping on the second layer
self
.
layerView2
.
layer
.
masksToBounds
=
YES
;
}
@end