CNLiveRoundedRectView.swift 3.48 KB
//
//  CNLiveRoundedRectView.swift
//  metaCode
//
//  Created by zx on 2023/11/14.
//

import Foundation
import CoreGraphics

let DS_RRV_ONE_PIXEL = 1.0 / UIScreen.main.scale

class CNLiveRoundedRectView: UIView {
    
    //填充色
    var _fillColor:UIColor?
    var fillColor:UIColor?{
        get{
            _fillColor
        }
        set{
            _fillColor = newValue
            self.setNeedsDisplay()
        }
    }
    //边框色
    var _strokeColor:UIColor?
    var strokeColor:UIColor?{
        get{
            _strokeColor
        }
        set{
            _strokeColor = newValue
            self.setNeedsDisplay()
        }
    }
    //边框宽度
    var _strokeWidth:CGFloat = 0.0
    var strokeWidth:CGFloat{
        get{
            _strokeWidth
        }
        set{
            _strokeWidth = newValue
            self.setNeedsDisplay()
        }
    }
    
    //圆角度数 默认0
    var _radius:CGFloat = 0.0
    var radius:CGFloat{
        get{
            _radius
        }
        set{
            _radius = newValue
            self.setNeedsDisplay()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
    }

    override var frame:CGRect{
        didSet {
            let newFrame = frame
            super.frame = newFrame
            self.setNeedsDisplay()
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        if ((self.strokeColor != nil && self.strokeColor?.cgColor != UIColor.clear.cgColor) && ( self.fillColor != nil && self.fillColor?.cgColor != UIColor.clear.cgColor))
        {
            let fpath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.radius)
            self.fillColor?.setFill()
            fpath.fill()
            let lineWidth = self.strokeWidth == 0 ? DS_RRV_ONE_PIXEL : self.strokeWidth
            let fx = lineWidth
            let fy = lineWidth
            let fwidth = self.width - fx*2
            let fheight = self.height - fy*2
            let spath = UIBezierPath(roundedRect: CGRect(x: fx, y: fy, width: fwidth, height: fheight), cornerRadius: self.radius-lineWidth)
            spath.lineWidth = lineWidth
            self.strokeColor?.setStroke()
            spath.stroke()
        }else if ((self.strokeColor != nil && self.strokeColor?.cgColor != UIColor.clear.cgColor) && (self.fillColor == nil || self.fillColor?.cgColor == UIColor.clear.cgColor)) {
            UIColor.clear.setFill()
            let lineWidth = self.strokeWidth == 0 ? DS_RRV_ONE_PIXEL : self.strokeWidth
            let x = lineWidth
            let y = lineWidth
            let width = self.width - x*2
            let height = self.height - y*2
            let path = UIBezierPath(roundedRect: CGRect(x: x, y: y, width: width, height: height), cornerRadius: self.radius-lineWidth)
            path.lineWidth = lineWidth
            self.strokeColor?.setStroke()
            path.stroke()
        }else if ((self.strokeColor == nil || self.strokeColor?.cgColor == UIColor.clear.cgColor) && (self.fillColor != nil && self.fillColor?.cgColor != UIColor.clear.cgColor)){
            UIColor.clear.setStroke()
            let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.radius)
            self.fillColor?.setFill()
            path.fill()
        }else{
            UIColor.clear.setStroke()
            UIColor.clear.setFill()
        }
    }
}