Graphics Reference
In-Depth Information
Rounded Corners, Redux
Chapter 2 mentioned that CAShapeLayer provides an alternative way to create a view
with rounded corners, as opposed to using the CALayer cornerRadius property.
Although using a CAShapeLayer is a bit more work, it has the advantage that it allows us
to specify the radius of each corner independently.
We could create a rounded rectangle path manually using individual straight lines and arcs,
but UIBezierPath actually has some convenience constructors for creating rounded
rectangles automatically. The following code snippet produces a path with three rounded
corners and one sharp:
//define path parameters
CGRect rect = CGRectMake ( 50 , 50 , 100 , 100 );
CGSize radii = CGSizeMake ( 20 , 20 );
UIRectCorner corners = UIRectCornerTopRight |
UIRectCornerBottomRight |
UIRectCornerBottomLeft ;
//create path
UIBezierPath *path = [ UIBezierPath bezierPathWithRoundedRect :rect
byRoundingCorners :corners
cornerRadii :radii];
We can use a CAShapeLayer with this path to create a view with mixed sharp and
rounded corners. If we want to clip the view's contents to this shape, we can use our
CAShapeLayer as the mask property of the view's backing layer instead of adding it as
a sublayer. (See Chapter 4, “Visual Effects,” for a full explanation of layer masks.)
CATextLayer
A user interface cannot be constructed from images alone. A well-designed icon can do a
great job of conveying the purpose of a button or control, but sooner or later you're going
to need a good old-fashioned text label.
If you want to display text in a layer, there is nothing stopping you from using the layer
delegate to draw a string directly into the layer contents with Core Graphics (which is
essentially how UILabel works). This is a cumbersome approach if you are working
directly with layers, though, instead of layer-backed views. You would need to create a
class that can act as the layer delegate for each layer that displays text, and then write logic
to determine which layer should display which string, not to mention keeping track of the
different fonts, colors, and so on.
Fortunately, this is unnecessary. Core Animation provides a subclass of CALayer called
CATextLayer that encapsulates most of the string drawing features of UILabel in layer
form and adds a few extra features for good measure.
 
Search WWH ::




Custom Search