Set uiview to rounded rectangle

Source: Internet
Author: User

Today, I want to make a button. I don't mean anything about using the system. I don't feel like I can't learn new things. So I made a uiview myself (I used to do Symbian for neural work, and I like to do it myself, make controls by yourself.

There are two ways to draw a rectangle with rounded corners:

1. Directly modify the view style. The system provides the following:

View. layer. cornerradius = 6;
View. layer. maskstobounds = yes;

It's easy to use layer. This requires inverted databaseQuartzcore. Framework;


2. Draw a rounded rectangle in the view

Cgfloat radius = 20.0;
Cgcontextref context = uigraphicsgetcurrentcontext ();
Cgcontextsetrgbstrokecolor (context, 1.0, 1.0, 1.0, 1 );
Cgfloat Minx = cgrectgetminx (rect), midx = cgrectgetmidx (rect), Maxx = cgrectgetmaxx (rect );
Cgfloat miny = cgrectgetminy (rect), midy = cgrectgetmidy (rect), Maxy = cgrectgetmaxy (rect );
Cgcontextmovetopoint (context, Minx, midy );
Cgcontextaddarctopoint (context, Minx, miny, midx, miny, radius );
Cgcontextaddarctopoint (context, Maxx, miny, Maxx, midy, radius );
Cgcontextaddarctopoint (context, Maxx, Maxy, midx, Maxy, radius );
Cgcontextaddarctopoint (context, Minx, Maxy, Minx, midy, radius );
Cgcontextclosepath (context );
Cgcontextdrawpath (context, kcgpathfill );


Use the paint brush method in drawrect.

I hope that everyone will not meet the current situation, so they will not be eliminated by using XIB and thinking more.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.