Method 1 (recommended): Use calayer
CALayer *middleBorder = [CALayer layer];middleBorder.frame = CGRectMake(x, y, width, height);middleBorder.backgroundColor = UIColor.CGColor;[myView.layer addSublayer:middleBorder];
Method 2: Use uiimageview (not easy to change)
1-(void) drawlinewithpoint :( cgpoint) startpoint topoint :( cgpoint) topoint 2 {3 cgsize screensize = [[uiscreen mainscreen] applicationframe]. size; 4 5 uiimageview * imageview = [[uiimageview alloc] initwithframe: cgrectmake (0, 0, screensize. width, screensize. height)]; 6 // uiimageview * imageview = [[uiimageview alloc] init]; 7 // imageview. frame = self. contentview. frame; 8 [self. contentview addsubview: imageview]; 9 10 uigraphicsbeginimagecontext (imageview. frame. size); 11 [imageview. image drawinrect: cgrectmake (0, 0, imageview. frame. size. width, imageview. frame. size. height)]; 12 13 // obtain the processing context 14 cgcontextref context = uigraphicsgetcurrentcontext (); 15 16 // specify the line style 17 cgcontextsetlinecap (context, kcglinecapsquare ); 18 19 // straight line width 20 cgcontextsetlinewidth (context, 1.0); 21 22 // set color 23 // RED: 166/255. 0 Green: 177/255. 0 Blue: 186/255. 024 cgcontextsetrgbstrokecolor (context, 246.0/255.0, 247.0/255.0, 247.0/255.0, 1.0); 25 26 // start to draw 27 cgcontextbeginpath (context ); 28 29 // the paint brush moves to the point (31,170) 30 cgcontextmovetopoint (context, startpoint. x, startpoint. y); 31 32 // 33 cgcontextaddlinetopoint (context, topoint. x, topoint. y); 34 35 // 36 cgcontextstrokepath (context); 37 38 imageview. image = uigraphicsgetimagefromcurrentimagecontext (); 39 uigraphicsendimagecontext (); 40 41 // nslog (@ "% F, % F", imageview. frame. size. width, imageview. frame. size. height); 42}
Draw a straight line