Draw a single solid line, the direction can be customized

Source: Internet
Author: User

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

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.