iOS set rounded and rounded pictures

Source: Internet
Author: User

Square style tends to appear very blunt, and the shape of the fillet will give people a different kind of intimacy, and now more and more use of rounded corners, such as the user's avatar and other pictures are mostly used to display the circle, this article describes how to set the button, the rounded corners of the text box and make a circular picture.


Let's take a look:


As shown, we've made a rounded avatar, a rounded round button, a small rounded button, and a label with a border and rounded corners.

The approximate idea is as follows:

View has a layer property, and we are using some of the layer's settings to achieve rounded corners, so view such as Uiimageview, UIButton, Uilabel and so on can set the corresponding fillet.


For a round head, to make a positive circle, we need to first set the Uiimageview of the width of the uniform, and then we set its fillet angle to the height divided by 2, equivalent to 90 degrees, the code is as follows:

    Round Head    Uiimageview *avatarimgview = [[Uiimageview alloc] Initwithframe:cgrectmake ((SCREENWIDTH-75)/2, 150, 75, 75 )];    Avatarimgview.image = [UIImage imagenamed:@ "icon"];    The picture is rounded, the fillet angle is half the height, and the high width is consistent, so it can form a positive circle    avatarImgView.layer.masksToBounds = YES;    AvatarImgView.layer.cornerRadius = AVATARIMGVIEW.FRAME.SIZE.HEIGHT/2;    [Self.view Addsubview:avatarimgview];


For the second full semicircular fillet, we also set the angle to the height of the general, so that can be on both sides of the half-circle, and because the width and height are different, so not the positive circle:

    Full semicircular rounded button    UIButton *btnone = [[UIButton alloc] Initwithframe:cgrectmake ((SCREENWIDTH-200)/2, (+), +)];
   [btnone settitle:@ "fully rounded" forstate:uicontrolstatenormal];    [Btnone Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];    [Btnone setbackgroundcolor:[uicolor colorwithred:228.0/255.0 green:117.0/255.0 blue:97.0/255.0 alpha:1.0]];    Button arc, with the height of half of the fillet, the two sides will form a complete semicircle    btnOne.layer.masksToBounds = YES;    BtnOne.layer.cornerRadius = BTNONE.FRAME.SIZE.HEIGHT/2;    [Self.view Addsubview:btnone];


For the third small rounded button, we directly set the roundness of the fillet as a number, the size of the number determines the style of the button fillet, here we set to 4:

    Small rounded button    UIButton *btntwo = [[UIButton alloc] Initwithframe:cgrectmake ((SCREENWIDTH-200)/2, 315, Max, +)];    [Btntwo settitle:@ "Small round Corner" forstate:uicontrolstatenormal];    [Btntwo Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];    [Btntwo setbackgroundcolor:[uicolor colorwithred:228.0/255.0 green:117.0/255.0 blue:97.0/255.0 alpha:1.0]];    Button arc, with the height of half of the fillet, the two sides will form a complete semicircle    btnTwo.layer.masksToBounds = YES;    BtnTwo.layer.cornerRadius = 4.0;    [Self.view Addsubview:btntwo];


The fourth label, we first want to add a border, we set the border width of 1, we can display the border, and then set the same fillet to 4:

    Rounded corners with borders label    UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake ((SCREENWIDTH-200)/2, 390, Max, max)];
   
    label.text = @ "Rounded with border label";    Label.textalignment = Nstextalignmentcenter;    Label.textcolor = [Uicolor colorwithred:228.0/255.0 green:117.0/255.0 blue:97.0/255.0 alpha:1.0];    Set Border Width    label.layer.borderWidth = 1.0;    Set border color    label.layer.borderColor = [[Uicolor colorwithred:228.0/255.0 green:117.0/255.0 blue:97.0/255.0 Alpha : 1.0] Cgcolor];    Set rounded corners    [Label.layer setcornerradius:4.0];    [Self.view Addsubview:label];
   


As above, the basic fillet setting is realized, very simple and practical.

Here is my example project, welcome Fork:https://github.com/cloudox/roundeddemo

iOS set rounded and rounded pictures

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.