oc和swift UIView类扩展画虚线外边框(含代码)

oc和swift UIView类扩展画虚线外边框

  • oc类扩展
  • swift类扩展
  • 在swift里面调用类扩展
    • 运行结果
  • 代码网址:请用xcode 12.2 以上运行

封装了oc和 swift的 2个UIView的虚线外框的类扩展,可以设置线的长度,间隙长度,虚线宽度,虚线颜色,外框圆角边框,一行代码调用非常方便

oc类扩展

//
//  UIView+Extension.h
//  画虚线
//
//  Created by tdw on 2020/12/2.
//

#import 

NS_ASSUME_NONNULL_BEGIN

@interface UIView (Extension)
/**
 画外框虚线
 
 @param width 虚线宽度
 @param lenth 一段虚线的长度
 @param space 虚线间距
 @param cornerRadius 边框圆角

 */
-(void) drawBoardDottedLine:(double)width lenth:(double)lenth space:(double)space cornerRadius:(double)cornerRadius color:(UIColor*)color;
@end

NS_ASSUME_NONNULL_END
@end



//
//  UIView+Extension.m
//  画虚线
//
//  Created by tdw on 2020/12/2.
//

#import "UIView+Extension.h"

@implementation UIView (Extension)
-(void) drawBoardDottedLine:(double)width lenth:(double)lenth space:(double)space cornerRadius:(double)cornerRadius color:(UIColor*)color
{
    self.layer.cornerRadius = cornerRadius;
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    
    borderLayer.bounds = self.bounds;
    borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:borderLayer.bounds cornerRadius:cornerRadius].CGPath;
    borderLayer.lineWidth = width / [[UIScreen mainScreen] scale];
    //虚线边框---小边框的长度
    borderLayer.lineDashPattern = @[@(lenth), @(space)];//前边是虚线的长度,后边是虚线之间空隙的长度
    borderLayer.lineDashPhase = 0.1;
    //实线边框
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = color.CGColor;
    [ self . layer addSublayer :borderLayer];
}
@end

swift使用使用的时候要在桥接文件里添加:

//画虚线-Bridging-Header.h
#import "UIView+Extension.h"

swift类扩展

//
//  UIView+Extension.swift
//  画虚线
//
//  Created by tdw on 2020/12/2.
//

import UIKit
extension UIView{
     
    func swiftDrawBoardDottedLine(width:CGFloat,lenth:CGFloat,space:CGFloat,cornerRadius:CGFloat,color:UIColor){
     
        self.layer.cornerRadius = cornerRadius
        let borderLayer =  CAShapeLayer()
        borderLayer.bounds = self.bounds
        
        borderLayer.position = CGPoint(x: self.bounds.midX, y: self.bounds.midY);
        borderLayer.path = UIBezierPath(roundedRect: borderLayer.bounds, cornerRadius: cornerRadius).cgPath
        borderLayer.lineWidth = width / UIScreen.main.scale
        
        //虚线边框---小边框的长度
        
        borderLayer.lineDashPattern = [lenth,space] as? [NSNumber] //前边是虚线的长度,后边是虚线之间空隙的长度
        borderLayer.lineDashPhase = 0.1;
        //实线边框
        
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.strokeColor = color.cgColor
        self.layer.addSublayer(borderLayer)
        
        
    }
}

在swift里面调用类扩展

//
//  ViewController.swift
//  画虚线
//
//  Created by tdw on 2020/12/2.
//

import UIKit

class ViewController: UIViewController {
     
    override func viewDidLoad() {
     
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let v1 = UIView()
        v1.frame = CGRect(x: 50, y: 50, width: 100, height: 100)
        v1.backgroundColor = UIColor.yellow
        //调用oc类扩展
        v1.drawBoardDottedLine(3, lenth: 5, space: 3, cornerRadius: 10, color: UIColor.red)
        view.addSubview(v1)
        
        
        let v2 = UIView()
        v2.frame = CGRect(x: 50, y: 200, width: 100, height: 100)
        v2.backgroundColor = UIColor.lightGray
        //调用swift类扩展
        v2.swiftDrawBoardDottedLine(width: 5, lenth: 4, space: 2, cornerRadius: 4 ,color:UIColor.orange)
        view.addSubview(v2)
        
        
    }


}

运行结果

oc和swift UIView类扩展画虚线外边框(含代码)_第1张图片

代码网址:请用xcode 12.2 以上运行

https://download.csdn.net/download/boildoctor/13582886

你可能感兴趣的:(swift,ios,objective,c)