Let's take a look at some of the rounded-arrow rectangle's Tip box effect chart that we've seen
First, understand Cgcontextref
First of all need to understand the Cgcontextref, the author has the opportunity to further explain in detail, this article in a simple introduction, convenient reading comprehension. Understand the Cgcontextref coordinate system first
Coordinates
Example: For the mall-type app has a lot of original price, the current comparison. How to draw the horizontal line of the original, you can use Cgcontextref
-(void) DrawRect: (cgrect) Rect {
//Drawing code
[Super Drawrect:rect];
Gets the text
cgcontextref context = Uigraphicsgetcurrentcontext ();
Set starting point coordinates (x axis: 0. Y axis: control height half)
cgcontextmovetopoint (context, 0, rect.size.height * 0.5);
Sets the line connection point (x-axis: The total length of the control. Y axis: control height half)
cgcontextaddlinetopoint (context, rect.size.width, rect.size.height * 0.5);
Sets the color width
[[Uicolor Redcolor] set];
Cgcontextsetlinewidth (context, 1);
Cgcontextstrokepath (context);
}
Description: from the above example you can see that a line is determined by two points.
Ii. Custom Uilabel
Into the body. Control can customize UIView. The author simply customizes the Uilabel for simplicity.
CustomLabel *customlabel = [[CustomLabel alloc] Initwithframe:cgrectmake (0, MB)];
[Self.view Addsubview:view];
View.backgroundcolor = [Uicolor Orangecolor];
First. h file
The. h file has nothing to say, the defined string will be explained later
#import <UIKit/UIKit.h>
@interface customlabel:uilabel
@property (nonatomic, copy) NSString * Fillcolorstr;
@end
. m file
We use- (void)drawRect:(CGRect)rect;
draw rounded arrows
#import "CustomLabel.h" @implementation CustomLabel-(Instancetype) initWithFrame: (CGRect) frame {self = [super Initwi
Thframe:frame];
if (self) {//Customize the attribute you need} return self;
}//main explanation-(void) DrawRect: (cgrect) Rect {//freely set a long width float w = rect.size.width-20;
float h = rect.size.height-20;
Gets the text cgcontextref context = Uigraphicsgetcurrentcontext ();
Set the edge width cgcontextsetlinewidth (context, 0.2); Border color Cgcontextsetstrokecolorwithcolor (context, [Uicolor Graycolor].
Cgcolor); Rectangle Fill Color Cgcontextsetfillcolorwithcolor (context, [Uicolor Cyancolor].
Cgcolor); /** first introduce cgcontextaddarctopoint parameter * Cgcontextref: for obtained text * x1, y1:1th * x2, y2:2nd * radius: Fillet radians * Description: Two-point lines like
Same vector line, directional.
*///[Start point] coordinates start at upper left (6, 0) cgcontextmovetopoint (context, 6, 0); Set 1th 2nd Radian. Detailed: [Start point] to [1th], determine a straight line (6, 0)-> (w, 0); [1th] to [2nd] determine a straight line (W, 0)-> (w, 10)//Two line connection with direction for Right-angle set Radian cgcontextaddarctopoint (context, W, 0, W, 10, 6); upper right corner arc set//Now [Start point] coordinate changes (W,10).-> (W, h) of the first line (W);
The second line (W, h)-> (w-10, h). Cgcontextaddarctopoint (Context, W, H, w-10, H, 6); Lower right corner arc set//Now [start point] coordinate becomes (w-10, h). Draw a straight line cgcontextaddlinetopoint (context, h) to the left (w-10, h)-> (h); Draw left/right now [start point] coordinate becomes (h). From (h)-> (H + 8) to the left to draw a straight line cgcontextaddlinetopoint (context,, H + 8); The lower left line//Now [Start point] coordinate changes (H + 8). Draw a straight line cgcontextaddlinetopoint (context, h) to the left of (->, H + 8); The top left line//Now [Start point] coordinate changes to (h). The first line (h)-> (0, h); The second line (0, h)-> (0, h-10) cgcontextaddarctopoint (context, 0, h, 0, h-10, 6); The lower left corner arc set//Now [Start point] coordinate changes (0, h-10). The first line (0, h-10)-> (0, 0); The second line (0, 0)-> (6, 0)//Description: The first set of coordinate points (6, 0) is not (0, 0). In order to finally be able to connect, otherwise the drawing line can not connect, the reader can test Cgcontextaddarctopoint (context, 0, 0, 6, 0, 6); The upper left corner arc set Cgcontextdrawpath (context, kcgpathfillstroke); Draw path based on coordinates
Three, simple encapsulation
See the top you can draw the rounded arrow rectangle
Remember the. h the attribute fillcolorstr defined, you can make calls, arbitrarily change the fill color, arc angle and so on.
Simple encapsulation (reader's own rigorous encapsulation)
-(void) DrawRect: (cgrect) Rect {///default fillet angle Float r = 4;
Center offset (arrowhead height) Float offset = 5;
Set arrow position float positionnum = 20;
Define coordinate point movement float Changenum = r + offset;
Set drawing line long width float w = self.frame.size.width;
float h = self.frame.size.height;
Gets the text cgcontextref context = Uigraphicsgetcurrentcontext ();
Set the edge width cgcontextsetlinewidth (context, 0.2); Border color Cgcontextsetstrokecolorwithcolor (context, [Uicolor Graycolor].
Cgcolor); Rectangle Fill Color if ([Self.fillcolorstr isequaltostring:@ "Fillcolorchange"]) {Cgcontextsetfillcolorwithcolor (context, [ Uicolor Bluecolor].
Cgcolor); }else{Cgcontextsetfillcolorwithcolor (context, [Uicolor Redcolor].
Cgcolor); } cgcontextmovetopoint (context, r, offset); Start coordinates to the left Cgcontextaddarctopoint (context, W, offset, W, Changenum, R); upper right corner Cgcontextaddarctopoint (context, W, H-offset, W-changenum, H-offset, R); lower Right angle cgcontextaddlinetopoint (context, Positionnum +, h-offset); Left Dash CgcontextaddlinetopoinT (context, Positionnum + 5, h); Downward slash cgcontextaddlinetopoint (context, positionnum, h-offset); Up slash cgcontextaddarctopoint (context, 0, H-offset, 0, H-changenum, R); Lower left angle cgcontextaddarctopoint (context, 0, offset, R, offset, R); Upper left corner angle Cgcontextdrawpath (context, kcgpathfillstroke);
/** the parent class call based on the coordinate drawing path after the line is drawn. Otherwise the text will be covered by * * [Super Drawrect:rect];
//When you want to change the fill color you can make call changes-(void) Setfillcolorstr: (NSString *) fillcolorstr{_fillcolorstr = Fillcolorstr; Call-(void) DrawRect: (cgrect) rect;
Redraw the fill color [self setneedsdisplay]; }
Set the control color to transparent, and a custom prompt box completes. The author only provides one method, the reader can also use uibezierpath etc. to realize.
Iv. Supplementary Notes
Some people say that directly with the picture to achieve, can, see the engineering needs. This article mainly talk about is
Use- (void)drawRect:(CGRect)rect;
draw rounded arrows
Introduction to the use of Cgcontextaddarctopoint
To use the picture realization, deformation problem.
UIImage class provides
(uiimage*) Stretchableimagewithleftcapwidth: (Nsinteger) Leftcapwidth topcapheight;
The simple explanation
Picture is 100 * 56 pixels
UIImage *tempimage = [uiimage imagenamed:@ "Tip.png"];
/** Description: Create a picture in which the content can be stretched and the corners are not stretched, in pixels
* Leftcapwidth: The width of the area on the left is not stretched
* Topcapheight: The height of the area is not stretched
, and the corner is not changed until the region is drawn horizontally
*/
tempimage = [Tempimage stretchableimagewithleftcapwidth:80 topcapheight:20];
Uiimageview *imgview=[[uiimageview Alloc]initwithimage:tempimage];
Imgview.frame = CGRectMake (M, M,);
[Self. View Addsubview:imgview];
After stretching, compare with original artwork
Summarize
The above is the entire content of this article, I hope the content of this article for your iOS developers can help, if you have questions you can message exchange.