Many places in the app used the solid line, some horizontal, some vertical, lazy when the direct use of UIView, set the background color is done ... However, the mind is very uneasy & uncomfortable. The code is on the bottom.
SingleLineView.h
1 #import<UIKit/UIKit.h>2 3 //single solid line direction4typedefenum {5 //Horizontal6 singlelineorientationvertical,7 //Portrait8 Singlelineorientationhorizontal9 } singlelineorientation;Ten One @interfaceSinglelineview:uiview { A cgfloat _linewidth; -Uicolor *_linecolor; - singlelineorientation _lineorientation; the } - - //single solid line width - @property (nonatomic) cgfloat linewidth; + //single solid Line Color -@property (nonatomic, strong) Uicolor *LineColor; + //single solid line direction A @property (nonatomic) singlelineorientation lineorientation; at - @end
singlelineview.m
1 #import "SingleLineView.h"2 3 @implementationSinglelineview4 5-(instancetype) init {6Self =[Super init];7 8 //single solid line style by default9 if(self) {Ten[Self setlinewidth:0.5]; One [self setlinecolor:[uicolor graycolor]]; A [self setlineorientation:singlelineorientationhorizontal]; - [self setbackgroundcolor:[uicolor clearcolor]]; - } the - returnSelf ; - } - + //Only override Drawrect:if perform custom drawing. - //An empty implementation adversely affects performance during animation. +- (void) DrawRect: (cgrect) rect { ACgcontextref context =Uigraphicsgetcurrentcontext (); at Cgcontextsetlinecap (context, kcglinecapround); - cgcontextsetlinewidth (context, self.linewidth); - cgcontextsetallowsantialiasing (context, YES); - Cgcontextsetstrokecolorwithcolor (context, self.lineColor.CGColor); - Cgcontextbeginpath (context); - inCgcontextmovetopoint (Context,0,0); - if(Self.lineorientation = =singlelineorientationhorizontal) { toCgcontextaddlinetopoint (context, Self.frame.size.width,0); +}Else { -Cgcontextaddlinetopoint (Context,0, self.frame.size.height); the } * $ Cgcontextstrokepath (context);Panax Notoginseng } - the #pragmaMark-getters && Setters + A-(cgfloat) linewidth { the return_linewidth; + } - $- (void) Setlinewidth: (cgfloat) linewidth { $_linewidth =linewidth; - [self setneedsdisplay]; - } the --(Uicolor *) LineColor {Wuyi return_linecolor; the } - Wu- (void) Setlinecolor: (Uicolor *) LineColor { -_linecolor =LineColor; About [self setneedsdisplay]; $ } - --(singlelineorientation) lineorientation { - return_lineorientation; A } + the- (void) Setlineorientation: (singlelineorientation) lineorientation { -_lineorientation =lineorientation; $ } the the @end
How to use:
Self.singlelinetest = [[Singlelineview alloc] init]; // [Self.singlelinetest setlineorientation:singlelineorientationvertical]; [Self.view addSubview:self.singleLineTest]; [self.singlelinetest mas_makeconstraints:^ (Masconstraintmaker * Make) { make.left.equalTo (self.view). With.offset (+); Make.right.equalTo (Self.view). With.offset (-); Make.top.equalTo (Self.view). With.offset (+); Make.height.mas_equalTo (1); }];
Above.
Draw a single solid line, the direction can be customized