iOS開發(fā) 給View添加指定位置的邊框線

略微封裝了一下跟匆,給View添加指定位置的邊框線,其中位移枚舉的使用詢問了哥們兒豪墅,總算搞定纤泵;
封裝一:直接封裝成了一個(gè)方法

/// 邊框類型(位移枚舉)
typedef NS_ENUM(NSInteger, UIBorderSideType) {
    UIBorderSideTypeAll    = 0,
    UIBorderSideTypeTop    = 1 << 0,
    UIBorderSideTypeBottom = 1 << 1,
    UIBorderSideTypeLeft   = 1 << 2,
    UIBorderSideTypeRight  = 1 << 3,
};

/**
 設(shè)置view指定位置的邊框

 @param originalView   原view
 @param color          邊框顏色
 @param borderWidth    邊框?qū)挾? @param borderType     邊框類型 例子: UIBorderSideTypeTop|UIBorderSideTypeBottom
 @return  view
 */
- (UIView *)borderForView:(UIView *)originalView color:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType {
    
    if (borderType == UIBorderSideTypeAll) {
        originalView.layer.borderWidth = borderWidth;
        originalView.layer.borderColor = color.CGColor;
        return originalView;
    }
    
    /// 線的路徑
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    
    /// 左側(cè)
    if (borderType & UIBorderSideTypeLeft) {
        /// 左側(cè)線路徑
        [bezierPath moveToPoint:CGPointMake(0.0f, originalView.frame.size.height)];
        [bezierPath addLineToPoint:CGPointMake(0.0f, 0.0f)];
    }
    
    /// 右側(cè)
    if (borderType & UIBorderSideTypeRight) {
        /// 右側(cè)線路徑
        [bezierPath moveToPoint:CGPointMake(originalView.frame.size.width, 0.0f)];
        [bezierPath addLineToPoint:CGPointMake( originalView.frame.size.width, originalView.frame.size.height)];
    }
    
    /// top
    if (borderType & UIBorderSideTypeTop) {
        /// top線路徑
        [bezierPath moveToPoint:CGPointMake(0.0f, 0.0f)];
        [bezierPath addLineToPoint:CGPointMake(originalView.frame.size.width, 0.0f)];
    }
    
    /// bottom
    if (borderType & UIBorderSideTypeBottom) {
        /// bottom線路徑
        [bezierPath moveToPoint:CGPointMake(0.0f, originalView.frame.size.height)];
        [bezierPath addLineToPoint:CGPointMake( originalView.frame.size.width, originalView.frame.size.height)];
    }
  
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = color.CGColor;
    shapeLayer.fillColor  = [UIColor clearColor].CGColor;
    /// 添加路徑
    shapeLayer.path = bezierPath.CGPath;
    /// 線寬度
    shapeLayer.lineWidth = borderWidth;
    
    [originalView.layer addSublayer:shapeLayer];
    
    return originalView;
}

封裝二:封裝成了類別

/// .h內(nèi)容
#import <UIKit/UIKit.h>

typedef NS_OPTIONS(NSUInteger, UIBorderSideType) {
    UIBorderSideTypeAll  = 0,
    UIBorderSideTypeTop = 1 << 0,
    UIBorderSideTypeBottom = 1 << 1,
    UIBorderSideTypeLeft = 1 << 2,
    UIBorderSideTypeRight = 1 << 3,
};

@interface UIView (BorderLine)

- (UIView *)borderForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType;

@end

/// .m內(nèi)容

#import "UIView+BorderLine.h"

@implementation UIView (BorderLine)

- (UIView *)borderForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType {
    
    if (borderType == UIBorderSideTypeAll) {
        self.layer.borderWidth = borderWidth;
        self.layer.borderColor = color.CGColor;
        return self;
    }
    
    
    /// 左側(cè)
    if (borderType & UIBorderSideTypeLeft) {
        /// 左側(cè)線路徑
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.f, 0.f) toPoint:CGPointMake(0.0f, self.frame.size.height) color:color borderWidth:borderWidth]];
    }
    
    /// 右側(cè)
    if (borderType & UIBorderSideTypeRight) {
        /// 右側(cè)線路徑
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(self.frame.size.width, 0.0f) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
    }
    
    /// top
    if (borderType & UIBorderSideTypeTop) {
        /// top線路徑
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, 0.0f) toPoint:CGPointMake(self.frame.size.width, 0.0f) color:color borderWidth:borderWidth]];
    }
    
    /// bottom
    if (borderType & UIBorderSideTypeBottom) {
        /// bottom線路徑
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, self.frame.size.height) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
    }
    
    return self;
}

- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth {

    /// 線的路徑
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:p0];
    [bezierPath addLineToPoint:p1];
    
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = color.CGColor;
    shapeLayer.fillColor  = [UIColor clearColor].CGColor;
    /// 添加路徑
    shapeLayer.path = bezierPath.CGPath;
    /// 線寬度
    shapeLayer.lineWidth = borderWidth;
    return shapeLayer;
}

@end

用法:

UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(80.0f, 80.0f, 200.0f, 100.0f)];
    testView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:testView];
    [self borderForView:testView color:[UIColor redColor] borderWidth:1.0f borderType:UIBorderSideTypeLeft | UIBorderSideTypeTop | UIBorderSideTypeBottom];

效果:

C1846502-36A3-4BD0-A118-D808444D09A0.png

不足之處:
邊框線過寬的話,交界處會(huì)有留白笔呀;

PS:第一次用簡書發(fā)文章幢踏,常用CSDN,我的CSDN看看相關(guān)iOS開發(fā)文章许师,都是平時(shí)我遇到的問題房蝉,解決了,記錄下來微渠,CSDN地址:http://blog.csdn.net/syg90178aw

注意搭幻,需要先把你的view加載在父view上,[self.view addSubview:testView]; 之后再設(shè)置邊框逞盆;否則可能會(huì)不起作用的檀蹋;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市云芦,隨后出現(xiàn)的幾起案子俯逾,更是在濱河造成了極大的恐慌,老刑警劉巖舅逸,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件桌肴,死亡現(xiàn)場離奇詭異,居然都是意外死亡琉历,警方通過查閱死者的電腦和手機(jī)坠七,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來旗笔,“玉大人彪置,你說我怎么就攤上這事』煌牛” “怎么了悉稠?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長艘包。 經(jīng)常有香客問我的猛,道長,這世上最難降的妖魔是什么想虎? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任卦尊,我火速辦了婚禮,結(jié)果婚禮上舌厨,老公的妹妹穿的比我還像新娘岂却。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布躏哩。 她就那樣靜靜地躺著署浩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪扫尺。 梳的紋絲不亂的頭發(fā)上筋栋,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機(jī)與錄音正驻,去河邊找鬼弊攘。 笑死,一個(gè)胖子當(dāng)著我的面吹牛姑曙,可吹牛的內(nèi)容都是我干的襟交。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼伤靠,長吁一口氣:“原來是場噩夢啊……” “哼捣域!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起醋界,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤竟宋,失蹤者是張志新(化名)和其女友劉穎提完,沒想到半個(gè)月后形纺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡徒欣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年逐样,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片打肝。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡脂新,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出粗梭,到底是詐尸還是另有隱情争便,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布断医,位于F島的核電站滞乙,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏鉴嗤。R本人自食惡果不足惜斩启,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望醉锅。 院中可真熱鬧兔簇,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至狸窘,卻和暖如春甚纲,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背朦前。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工介杆, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人韭寸。 一個(gè)月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓春哨,卻偏偏與公主長得像,于是被迫代替她去往敵國和親恩伺。 傳聞我的和親對象是個(gè)殘疾皇子赴背,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容