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