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

  • 相关阅读:
    php数据库常用函数
    什么是RESTful API
    RESTful API 设计指南
    json和jsonp的使用区别
    Memcached, Redis, MongoDB区别
    入门系列之在Nginx配置Gzip
    100行代码搞定抖音短视频App,终于可以和美女合唱了。
    游戏开发者注意!这个音频SDK可以完美兼容所有主流游戏引擎
    快速上手:在CVM上安装Apache
    聚焦小游戏技术生态,腾讯游戏云GAME-TECH落地厦门
  • 原文地址:https://www.cnblogs.com/yujidewu/p/6184052.html
Copyright © 2020-2023  润新知