Implementation of dashed borders

Source: Internet
Author: User

1. Create class inheritance UIImage;

2. Create a method for plotting dashed lines:

+ (ID) imagewithsize: (cgsize) Size bordercolor: (Uicolor *) color borderWidth: (cgfloat) BorderWidth
{
Open Picture Context
Uigraphicsbeginimagecontextwithoptions (Size, NO, 0.0);
Set color
[[Uicolor Clearcolor] set];
Get Current context
Cgcontextref context = Uigraphicsgetcurrentcontext ();
Cgcontextbeginpath (context);
Setting properties
Cgcontextsetlinewidth (context, borderWidth);
Cgcontextsetstrokecolorwithcolor (context, color. Cgcolor);
CGFloat lengths[] = {3, 1};
Cgcontextsetlinedash (context, 0, lengths, 1);
Cgcontextmovetopoint (context, 0.0, 0.0);
Cgcontextaddlinetopoint (context, size.width, 0.0);
Cgcontextaddlinetopoint (context, size.width, size.height);
Cgcontextaddlinetopoint (context, 0, size.height);
Cgcontextaddlinetopoint (context, 0.0, 0.0);
Draw
Cgcontextstrokepath (context);
Gets the context of the drawn picture
uiimage* image = Uigraphicsgetimagefromcurrentimagecontext ();
Close Picture Context
Uigraphicsendimagecontext ();

return image;
}

3. External calls. Remember: To call with a class method (good carelessness ...). At first do not use the class method call, resulting in the case of stuck in the method can not be adjusted ....)

-(void) Viewdidload {
[Super Viewdidload];

Implementing a dashed border class method call

UIImage *img = [Creatimage imagewithsize:cgsizemake (a) Bordercolor:[uicolor Redcolor] borderwidth:3];

Uiimageview *IMG1 = [[Uiimageview alloc]initwithframe:cgrectmake (100, 100, 100, 100)];
[Img1 setimage:img];
[Self.view ADDSUBVIEW:IMG1];

}

I also according to other people's blog practice to do, learn to learn. Oh.....

Implementation of dashed borders

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.