UIView Draw Dashed Line

Source: Internet
Author: User
Tags dashed line

- (void) drawline{Cashapelayer*shapelayer =[Cashapelayer layer];    [Shapelayer SetBounds:self.bounds];    [Shapelayer SetPosition:self.center];    [Shapelayer Setfillcolor:[[uicolor Clearcolor] cgcolor]; //set the dashed line color to Blackcolor [Shapelayer Setstrokecolor:[[uicolor Blackcolor] cgcolor]];[Shapelayer setstrokecolor:[[uicolor colorwithred:223/255.0Green223/255.0Blue223/255.0Alpha1.0f] [Cgcolor]]; //3.0f Set the width of the dashed line[Shapelayer setlinewidth:1.0f];    [Shapelayer Setlinejoin:kcalinejoinround]; //3 = width of the line 1 = spacing per line[Shapelayer setlinedashpattern:[nsarray arraywithobjects:[nsnumber Numberwithint:1],[nsnumber Numberwithint:1],nil]]; //Setup the pathCgmutablepathref Path =cgpathcreatemutable (); //0, 10 represents the X, y of the initial coordinate//320, 10 represents the X, y of the initial coordinateCgpathmovetopoint (Path, NULL,0,0); Cgpathaddlinetopoint (Path, NULL,0, the);     [Shapelayer Setpath:path];    Cgpathrelease (path); [_verticalline.layer Addsublayer:shapelayer];}

Or

#import <UIKit/UIKit.h>@interface linedashview:uiview@property (nonatomic, strong) Nsarray   * Linedashpattern;  // Segment Segmentation mode @property (nonatomic, assign) cgfloat    Endoffset;        // value between 0.001 and 0.499 -(Instancetype) initWithFrame: (CGRect) frame linedashpattern: (Nsarray *) Linedashpattern Endoffset: (cgfloat) endoffset; @end
#import "LineDashView.h"@interfaceLinedashview () {Cashapelayer*_shapelayer;}@end@implementationLinedashview- (ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {Uibezierpath*path =[Uibezierpath bezierPathWithRect:self.bounds]; _shapelayer= (Cashapelayer *) Self.layer; _shapelayer.fillcolor=[Uicolor Clearcolor].        Cgcolor; _shapelayer.strokestart=0.001; _shapelayer.strokeend=0.499; _shapelayer.linewidth=Frame.size.width; _shapelayer.path=path.    Cgpath; }    returnSelf ;}-(Instancetype) initWithFrame: (CGRect) frame linedashpattern: (Nsarray*) Linedashpattern Endoffset: (cgfloat) endoffset{Linedashview*linedashview =[[Linedashview alloc] initwithframe:frame]; Linedashview.linedashpattern=Linedashpattern; Linedashview.endoffset=Endoffset; returnLinedashview;}#pragmaMark-Modify View's Backedlayer to cashapelayer+(Class) layerclass{return[Cashapelayerclass];}#pragmaMark-Getter,setter method for overriding attributes@synthesizeLinedashpattern =_linedashpattern;- (void) Setlinedashpattern: (Nsarray *) linedashpattern{_linedashpattern=Linedashpattern; _shapelayer.linedashpattern=Linedashpattern;}-(Nsarray *) linedashpattern{return_linedashpattern;}@synthesizeEndoffset =_endoffset;- (void) Setendoffset: (cgfloat) endoffset{_endoffset=Endoffset; if(Endoffset <0.499&& Endoffset >0.001) {_shapelayer.strokeend=_endoffset; }}-(cgfloat) endoffset{return_endoffset;}#pragmaMark-Rewritten the system's BackgroundColor property-(void) SetBackgroundColor: (Uicolor *) backgroundcolor{_shapelayer.strokecolor=Backgroundcolor.cgcolor;}-(Uicolor *) backgroundcolor{return[Uicolor Colorwithcgcolor:_shapelayer.strokecolor];}@end
1 linedashpattern:@[@1, @1] Endoffset:0.495]    ; = [Uicolor redcolor];    [Self.contentview addsubview:line1];

UIView Draw Dashed Line

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.