//  참고사이트 :  https://github.com/oper0960/ExtensionsForSwift/blob/master/ExtensionsForSwift/Classes/CALayerExtension.swift

import UIKit

public extension CALayer {
    static let kLayerNameTopBorder = "TopBorderLayer"
    static let kLayerNameBottomBorder = "BottomBorderLayer"
    static let kLayerNameLeftBorder = "LeftBorderLayer"
    static let kLayerNameRightBorder = "RightBorderLayer"
    
    // Create a border for each direction in the view
    func setBorder(_ arr_edge: [UIRectEdge], color: UIColor, thickness: CGFloat) {
        
        for edge in arr_edge {
            let border = CALayer()
            switch edge {
            case UIRectEdge.top:
                border.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: thickness)
                break
            case UIRectEdge.bottom:
                border.frame = CGRect.init(x: 0, y: frame.height - thickness, width: frame.width, height: thickness)
                break
            case UIRectEdge.left:
                border.frame = CGRect.init(x: 0, y: 0, width: thickness, height: frame.height)
                break
            case UIRectEdge.right:
                border.frame = CGRect.init(x: frame.width - thickness, y: 0, width: thickness, height: frame.height)
                break
            default:
                break
            }
            border.backgroundColor = color.cgColor;
            self.addSublayer(border)
        }
    }
    
    func setTopBorder(color: UIColor, thickness: CGFloat) {
        let border = CALayer()
        border.name = CALayer.kLayerNameTopBorder
        border.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: thickness)
        border.backgroundColor = color.cgColor;
        
        removeLayer(layername: border.name!)
        
        self.addSublayer(border)
    }
    
    func setBottomBorder(color: UIColor, thickness: CGFloat) {
        let border = CALayer()
        border.name = CALayer.kLayerNameBottomBorder
        border.frame = CGRect.init(x: 0, y: frame.height - thickness, width: frame.width, height: thickness)
        border.backgroundColor = color.cgColor;
        
        removeLayer(layername: border.name!)
        
        self.addSublayer(border)
    }
    
    func setLeftBorder(color: UIColor, thickness: CGFloat) {
        let border = CALayer()
        border.name = CALayer.kLayerNameLeftBorder
        border.frame = CGRect.init(x: 0, y: 0, width: thickness, height: frame.height)
        border.backgroundColor = color.cgColor;
        
        removeLayer(layername: border.name!)
        
        self.addSublayer(border)
    }
    
    func setRightBorder(color: UIColor, thickness: CGFloat) {
        let border = CALayer()
        border.name = CALayer.kLayerNameRightBorder
        border.frame = CGRect.init(x: frame.width - thickness, y: 0, width: thickness, height: frame.height)
        border.backgroundColor = color.cgColor;
        
        removeLayer(layername: border.name!)
        
        self.addSublayer(border)
    }
    
    // Set border by Default library simplification
    func setBorderColor(_ width: CGFloat, color: UIColor) {
        self.borderWidth = width
        self.borderColor = color.cgColor
    }
    
    func removeLayer(layername: String) {
        if let sublayers = self.sublayers {
               for layer: CALayer in sublayers {
                   if (layer.name == layername) {
//                    log(direction: .UI, ofType: self, datas: "=> UIView removeLayer \(String(describing: layername))")
                       layer.removeFromSuperlayer()
                       break
                   }
               }
        }
    }
}

 

사용방법

// 상단만 Border 컬러 적용
view.layer.setTopBorder(color: UIColor.whiteTwo, thickness: 2.0)

+ Recent posts