Graphics Reference
In-Depth Information
The header, shown in Listing 11-3,
retains a reference to the
CATextLayer
sublayer, which enables you to adjust its
text as needed. We also have a reference
to the associated color object. The
header also includes a pair of accessors,
-string
and
-setString
, which set the
strings on the
CATextLayer
sublayer.
Finally, the
-setSelected
method
informs the layer when it is actively
being clicked.
WARNING
Because
CATextLayer
objects cannot be
centered vertically, you cannot build the
buttons on a single layer. Therefore, each
button needs to be in its own layer so that
we can place each one where we want it.
LISTING 11-3
LZButtonLayer Header File
#import
<Cocoa/Cocoa.h>
@interface
LZButtonLayer : CALayer
{
__weak
CATextLayer
*
textLayer
;
CGColorRef myColor
;
}
@property
(
assign
) CGColorRef myColor;
- (
NSString
*)string;
- (
void
)setString:(
NSString
*)string;
- (
void
)setSelected:(
BOOL
)selected;
@end
Whenever
[CALayer layer]
is called, the
init
method is also called as the default initial-
izer, as shown in Listing 11-4. The button layer overrides the default initializer and
configures the button itself. When the
[super init]
finishes its task, the background
layer of the button is configured by setting its
cornerRadius
,
bounds
,
borderWidth
, and
borderColor
.
NOTE
Next, the
textLayer
is initialized. Even
though the
textLayer
is an auto-
released object (because we did not call
alloc
or
copy
when we created it), we
continue referencing it. Because we
define this as a weak reference, we are
not retaining it but instead letting the
layer hierarchy handle its retention.
With the
CATextLayer
initialized, the
Weak References
Weak references were added in Mac OS X
Leopard (v 10.5) and effectively zero out the
reference if the referenced object is
released. This is primarily used when the
garbage collector is turned on, but it is a
helpful flag to use when doing non-GC devel-
opment (such as for the iPhone) as well.