Fun with layer

來源:互聯網
上載者:User
origin link: http://bynomial.com/blog/?p=52Bynomial CodeTips and thoughts for iPhone OS coders.Fun with layers

In this post, I’ll explain how to add a border, rounded corners, and drop shadow to any UIView using some simple CALayerproperties.  I’m not a CALayer guru, but these few tricks from the layer world are particularly nice to know about.

These properties are present for every UIView, since every view is actually drawn using an underlying CALayer object owned by the UIView.  You can do a lot without even knowing about CALayers because UIViews encapsulate a lot of their functionality.  These properties, however, are useful pieces that are not available directly through the UIView interface.

To use these properties, you need to include the QuartzCore header:

Borders

To get a border, just set the borderColor and borderWidth properties of the layer, for example:

The borderColor is a CGColorRef, which you can easily extract from any UIColor as in the above example, which generates a border like this:

The border is just inside the frame of the view.  Fractional values are allowed for the borderWidth as well.

Corners

You can create rounded corners with code like this:

just the cornerRadius property is needed; I’ve also set the border to show how these properties work together:

As you can see in the example screenshot, the backgroundColor of the UIView is also restricted by the corner radius.  You need to have clipsToBounds set to YES on your UIView for rounded corners to work.

Shadows

You can also create a drop shadow that will be based on the alpha component of whatever is drawn in your view.  Often this will result in a shadow just around the edges of the view.  This example code on a UILabel:

results in this appearance:

In this case, you need clipsToBounds to be NO in order for a shadow outside the frame of your view to show up.  Next I’ll show you how you can actually combine rounded corners and drop shadows, since I’m sure that’s what you really want to do now.

All together

Let’s say you want to present an image with a border, rounded corners, and a drop shadow.  The obvious problem from the above explanations is that clipsToBounds needs to be YES for the rounded corners to work and NO for the drop shadows.  What’s a coder to do?

We can get around this apparent paradox by using the fact that the CALayer treats its own background color (which may be image-based) differently than the UIView‘s background color.  Specifically, we can set clipsToBounds to NO and still achieve rounded corners by using direct properties of the layer instead of the UIView.  This code:

Generates the image on the right, using the left image as the source (mandel.png):

Reference

I originally learned about this stuff from this blog post.

CALayer class reference

This entry was written by Tyler, posted on May 8, 2010 at 3:14 pm, filed under UI code tips and tagged border, drop shadow, rounded corners, UIView. Bookmark thepermalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.