示例代码

///////////////////////////OC.h//////////////////////////

//
//  UIView+FreeBorder.h
//  BHBFreeBorder
//
//  Created by bihongbo on 15/12/30.
//  Copyright © 2015年 bihongbo. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
    BorderTypeTop,
    BorderTypeLeft,
    BorderTypeRight,
    BorderTypeBottom
} BorderType;

@interface UIView (FreeBorder)

/** 多边框 */
- (void)addBorderWithColor:(UIColor *)color size:(CGFloat)size borderTypes:(NSArray *)types;

/** 单边框 */
- (void)addBorderLayerWithColor:(UIColor *)color size:(CGFloat)size borderType:(BorderType)boderType;

@end

//示例
/// 下上右边框
//[lbl1 addBorderWithColor:[UIColor redColor] size:1 borderTypes:@[@(BorderTypeBottom),@(BorderTypeTop),@(BorderTypeRight)]];
/// 单根边框
//[lbl2 addBorderLayerWithColor:[UIColor greenColor] size:1 borderType:BorderTypeRight];

///////////////////////////OC.m//////////////////////////

//
//  UIView+FreeBorder.m
//  BHBFreeBorder
//
//  Created by bihongbo on 15/12/30.
//  Copyright © 2015年 bihongbo. All rights reserved.
//

#import "UIView+FreeBorder.h"

@implementation UIView (FreeBorder)

- (void)addBorderWithColor:(UIColor *)color size:(CGFloat)size borderTypes:(NSArray *)types{
    for (int i = 0 ; i < types.count; i ++) {
        [self addBorderLayerWithColor:color size:size borderType:[types[i] integerValue]];
    }
}

- (void)addBorderLayerWithColor:(UIColor *)color size:(CGFloat)size borderType:(BorderType)boderType{
    CALayer * layer = [CALayer layer];
    layer.backgroundColor = color.CGColor;
    [self.layer addSublayer:layer];
    
    switch (boderType) {
        case BorderTypeTop:
            layer.frame = CGRectMake(0, 0, self.frame.size.width, size);
            break;
        case BorderTypeLeft:
            layer.frame = CGRectMake(0, 0, size, self.frame.size.height);
            break;
        case BorderTypeBottom:
            layer.frame = CGRectMake(0, self.frame.size.height - size, self.frame.size.width, size);
            break;
        case BorderTypeRight:
            layer.frame = CGRectMake(self.frame.size.width - size, 0, size, self.frame.size.height);
            break;
        default:
            break;
    }

}

@end

///////////////////////////Swift//////////////////////////

//
//  UIView+FreeBolder.swift
//  daydays
//
//  Created by bihongbo on 9/14/15.
//  Copyright (c) 2015 daydays. All rights reserved.
//

import Foundation
import UIKit

@objc enum BorderType:NSInteger{
    
    case top,left,bottom,right
    
}
extension UIView{
    
    // MARK: - 为视图加上边框 ,枚举数组可以填充上下左右四个边
    @objc func addBorder(color: UIColor?, size: CGFloat, borderTypes:NSArray){
        
        var currentColor:UIColor?
        
        if let _ = color{
            currentColor = color
        }else{
            currentColor = UIColor.blackColor()
        }
        for borderType in borderTypes{
            let bt: NSNumber = borderType as! NSNumber
            self.addBorderLayer(currentColor!, size: size, boderType: BorderType(rawValue: bt.integerValue)!)
        }
    }
    
    @objc func addBorderLayer(color: UIColor, size: CGFloat, boderType: BorderType){
        
        let layer:CALayer = CALayer()
        layer.backgroundColor = color.CGColor
        self.layer.addSublayer(layer)
        
        switch boderType{
            
        case .top:
            layer.frame = CGRectMake(0, 0, self.frame.width, size)
            
        case .left:
            layer.frame = CGRectMake(0, 0, size, self.frame.height)
            
        case .bottom:
            layer.frame = CGRectMake(0, self.frame.height - size, self.frame.width, size)
            
        case .right:
            layer.frame = CGRectMake(self.frame.width - size, 0, size, self.frame.height)
            
            //        default:
            //            return;
        }
    }
}

//示例
/// 下上右边框
//lbl1.addBorder(UIColor.redColor(), size: 1, borderTypes: [BorderType.bottom.rawValue,BorderType.top.rawValue,BorderType.right.rawValue])
/// 单根边框
//lbl2.addBorderLayer(UIColor.greenColor(), size: 1, boderType: BorderType.right);

最新文章

  1. log4Net不能成功生成日志问题(关于配置错误)
  2. Node.js高级编程读书笔记Outline
  3. HDU 5017 Ellipsoid 模拟退火第一题
  4. Android测试AsyncTask下载图片
  5. css技术和实例
  6. PHP读某一个目录下所有文件和文件夹
  7. TP复习14
  8. Spark-Java版本WordCount示例
  9. 【转载】iOS 设置Launch Image 启动图片(适用iOS9)
  10. SQL Count(*)函数,GROUP_By,Having的联合使用
  11. spring-security实现的token授权
  12. Web发展史
  13. codeforces742B
  14. Flask学习【第6篇】:Flask中的信号
  15. iOS:根据系统类型加载不同的xib
  16. laravel mapSpread 例子
  17. AIM Tech Round 5 (rated, Div. 1 + Div. 2)
  18. swift3.0 创建经典界面的九宫图
  19. selenium--特殊元素定位
  20. windows python easy_install ,pip. selenium

热门文章

  1. Partition:分区切换(Switch)
  2. 理解nodejs模块的scope
  3. “fixed+relative==absolute”——对BFC的再次思考
  4. 【Java学习系列】第3课--Java 高级教程
  5. CSS3 @keyframes 动画
  6. Struts2日期类型转换
  7. Spring MVC初始化参数绑定
  8. Linux文件查找.md
  9. 让ASP.NET5在Jexus上飞呀飞
  10. 吐血大奉献,打造cnblogs最新最火辣的css3模板(IE9以下请勿入内) -- 第一版