Customizing a View DrawLineDrawLine.h#import<UIKit/UIKit.h>@protocolGridtouchdelete <nsobject>-(void) Gridtouchcolumn: (nsinteger) column Touchrow: (nsinteger) row;@end@interfaceDrawline:uiview@property (nonatomic, assign)ID<gridTouchDelete>Delegate;@end#import "DrawLine.h"#defineGrid_width 44#defineGrid_height 44@implementationDrawLine- (ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {//Initialization Code } returnSelf ;}- (void) DrawRect: (cgrect) rect{cgcontextref context=Uigraphicsgetcurrentcontext (); //Grid Line ColorCgcontextsetstrokecolorwithcolor (context, [Uicolor Graycolor]. Cgcolor); Cgcontextbeginpath (context); Nsinteger NumRows=5; floatGridheight = numrows* (grid_height+2)+1; //Background ColorCGRect Rectanglegrid = CGRectMake (0,0, Self.frame.size.width,gridheight); Cgcontextaddrect (context, Rectanglegrid); Cgcontextsetfillcolorwithcolor (context, [[Uicolor Graycolor] Colorwithalphacomponent:0.2]. Cgcolor); Cgcontextfillpath (context); for(inti =0; I <7; i++) { //Columns ColumnCgcontextmovetopoint (Context, i* (grid_width+1) +i*1,0); Cgcontextaddlinetopoint (Context, I* (grid_width+1) +i*1, Gridheight); if(i > NumRows)Continue; //Rows RowCgcontextmovetopoint (Context,0, i* (grid_height+1) +i*1+1); Cgcontextaddlinetopoint (context, Self.frame.size.width,0+i* (grid_height+1) +i*1+1); } cgcontextstrokepath (context); Cgcontextsetallowsantialiasing (context, YES); Nsinteger Gridnum= NumRows *7; for(inti =0; i < Gridnum; i + +) { intTargetcolumn = i%7; intTargetrow = i/7; intTargetx = Targetcolumn * (grid_width+2)+ the; intTargety = Targetrow * (grid_height+2)+ the; NSString*GRIDSTR = [NSString stringWithFormat:@"%d", i+1]; if([[[[Uidevice Currentdevice] systemversion] Floatvalue] >=7.0) {Uicolor*FontColor; if(Targetcolumn = =0|| Targetcolumn = =6) {FontColor=[Uicolor Redcolor]; } Else{fontcolor=[Uicolor Blackcolor]; } [Gridstr Drawinrect:cgrectmake (Targetx, Targety, Self.frame.size.width, Grid_width) withattribute S:@{nsfontattributename:[uifont Fontwithname:@"Helveticaneue-bold"Size -],nsforegroundcolorattributename:fontcolor}]; } Else{[Gridstr drawinrect:cgrectmake (Targetx, Targety, Self.frame.size.width, Grid_width) withfont:[uif Ont fontwithname:@"Helveticaneue-bold"Size -] linebreakmode:nslinebreakbyclipping Alignment:nstextalignmentcenter]; Cgcontextsetfillcolorwithcolor (Context,[uicolor Redcolor]. Cgcolor); } }}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{Uitouch*touch =[touches anyobject]; Cgpoint TouchPoint=[Touch locationinview:self]; floatXlocation =Touchpoint.x; floatYlocation =Touchpoint.y; intColumn = Floorf (xlocation/(grid_height+2)); introw = Floorf (ylocation/(grid_width+2)); if([_delegate respondstoselector: @selector (gridtouchcolumn:touchrow:)]) {[_delegate gridtouchcolumn:column Touc Hrow:row]; }}mainviewcontroller- (void) viewdidload{[Super Viewdidload]; //Do any additional setup after loading the view.DrawLine*lineview = [[DrawLine alloc]initwithframe:cgrectmake (0, -, Self.view.frame.size.width, -)]; Lineview.backgroundcolor=[Uicolor Clearcolor]; Lineview.Delegate=Self ; [Self.view Addsubview:lineview];}- (void) Gridtouchcolumn: (nsinteger) column Touchrow: (nsinteger) row{NSLog (@"line:%d, column:%d", Column,row);}
DrawRect Drawing table for IOS