iOS 自訂方法,ios自訂方法

來源:互聯網
上載者:User

iOS 自訂方法,ios自訂方法

範例程式碼

///////////////////////////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);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.