iOS development graphics draw, draw lines, rectangles, and draw text vertically and centered

Source: Internet
Author: User
Tags border color

Overview

The graphics drawing under iOS in The Spit slot, the code is lengthy, and has to be re-packaged by itself. Collation forms this article.

Draw lines
// Draw line + (void) Todrawlinefromx: (cgfloat) x1 Y: (cgfloat) y1 ToX: (cgfloat) x2 ToY: (cgfloat) y2 context :(cgcontextref) con{    cgcontextmovetopoint (Con, x1, y1);    Cgcontextaddlinetopoint (Con, x2, y2);     1);    Cgcontextstrokepath (con);}

Draw a rectangle
//Draw Rectangle, FillColor fill Color+ (void) Todrawrect: (cgrect) Rectangle Color:fillcolor Context: (cgcontextref) ctx{//create a path and get a handlecgmutablepathref Path=cgpathcreatemutable (); //Add a rectangle to the pathcgpathaddrect (path,null, rectangle); //Get Context//to add a path to the contextCgcontextaddpath (CTX, path); //Set Rectangle fill Color[FillColor Setfill]; //Rectangle Border Color[[Uicolor Whitecolor] setstroke]; //Border WidthCgcontextsetlinewidth (ctx,0); //DrawingCgcontextdrawpath (CTX, Kcgpathfillstroke); Cgpathrelease (path);}

Draw text vertically and centered
///Draw Text, rect1 the specified rectangle, and draw the text horizontally and vertically centered in the rectangle+ (void) Todrawtextwithrect: (cgrect) rect1 str: (nsstring*) str1 Context: (cgcontextref) context{if(str1 = = Nil | | context = =Nil)return; Cgcontextsetlinewidth (Context,1.0); Cgcontextsetrgbfillcolor (Context,0.01, 0.01, 0.01, 1); //paragraph formatNsmutableparagraphstyle *textstyle =[[Nsmutableparagraphstyle Defaultparagraphstyle] mutablecopy]; Textstyle.linebreakmode=nslinebreakbywordwrapping; Textstyle.alignment= Nstextalignmentcenter;//Center Horizontally//FontUifont *font = [Uifont boldsystemfontofsize:22.0]; //building a collection of propertiesNsdictionary *attributes =@{nsfontattributename:font, Nsparagraphstyleattributename:textstyle}; //Get SizeCgsize strsize =[str1 sizewithattributes:attributes]; CGFloat margintop= (rect1.size.height-strsize.height)/2; //Center vertically to calculate for yourselfCGRect r = CGRectMake (rect1.origin.x, RECT1.ORIGIN.Y +margintop,rect1.size.width, strsize.height); [str1 drawinrect:r withattributes:attributes];}

How to use

Suppose you put the above method into a class in Drawutil, we can Drawutil to invoke the method.

Definition: #define DRAWLINE (X1,y1,x2,y2,con) [Drawutil todrawlinefromx:x1 y:y1 tox:x2 toy:y2 Context:con]

Get context

  Cgcontextref con = uigraphicsgetcurrentcontext();

cgcontextclearrect(con, rect);

Draw lines,

DrawLine(x,y,x+rectwidth, Y,con);

Rectangular

[drawutil todrawrect :cgrectmake (x*unitwidth+< Span class= "S5" >1unitheight+1 ,unitwidth-1unitheight-1) color :[uicolor whitecolor context:con];

Text

[drawutil todrawtextwithrect: rect1 str: @ "You" context: context];

iOS development graphics draw, draw lines, rectangles, and draw text vertically and centered

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.