示例代碼 ///////////////////////////OC.h////////////////////////// //// UIView+FreeBorder.h// BHBFreeBorder//// Created by bihongbo on 15/12/30.// Copyrig ...
///////////////////////////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);