Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
Created September 21, 2020 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodycatliu/48964ced9ae32897cd1848631f6439d6 to your computer and use it in GitHub Desktop.
Save woodycatliu/48964ced9ae32897cd1848631f6439d6 to your computer and use it in GitHub Desktop.
Swift - UIKit :繪製圓角Border
override func draw(_ rect: CGRect) {
borderColor.setStroke()
borderColor.withAlphaComponent(backgroundOpacity).setFill()
let backgroundPath = UIBezierPath(roundedRect: bounds, cornerRadius: borderCornerRadius)
backgroundPath.fill()
let borderPath: UIBezierPath
let borderRect = bounds.insetBy(dx: borderWidth / 2, dy: borderWidth / 2)
if borderCornerSize == 0 {
borderPath = UIBezierPath(roundedRect: borderRect, cornerRadius: borderCornerRadius)
} else {
var cornerSizeH = borderCornerSize
if cornerSizeH > borderRect.width / 2 - borderCornerRadius {
cornerSizeH = max(borderRect.width / 2 - borderCornerRadius, 0)
}
var cornerSizeV = borderCornerSize
if cornerSizeV > borderRect.height / 2 - borderCornerRadius {
cornerSizeV = max(borderRect.height / 2 - borderCornerRadius, 0)
}
let cornerSize = CGSize(width: cornerSizeH, height: cornerSizeV)
borderPath = UIBezierPath(cornersOfRect: borderRect, cornerSize: cornerSize, cornerRadius: borderCornerRadius)
}
borderPath.lineWidth = borderWidth
borderPath.stroke()
}
extension UIBezierPath {
convenience init(cornersOfRect borderRect: CGRect, cornerSize: CGSize, cornerRadius: CGFloat) {
//conrnerSize: 圓角邊長
self.init()
let cornerSizeH = cornerSize.width
let cornerSizeV = cornerSize.height
// top-left
move(to: CGPoint(x: borderRect.minX, y: borderRect.minY + cornerSizeV + cornerRadius))
addLine(to: CGPoint(x: borderRect.minX, y: borderRect.minY + cornerRadius))
addArc(withCenter: CGPoint(x: borderRect.minX + cornerRadius, y: borderRect.minY + cornerRadius),
radius: cornerRadius,
startAngle: CGFloat.pi,
endAngle: -CGFloat.pi / 2,
clockwise: true)
addLine(to: CGPoint(x: borderRect.minX + cornerSizeH + cornerRadius, y: borderRect.minY))
// top-right
move(to: CGPoint(x: borderRect.maxX - cornerSizeH - cornerRadius, y: borderRect.minY))
addLine(to: CGPoint(x: borderRect.maxX - cornerRadius, y: borderRect.minY))
addArc(withCenter: CGPoint(x: borderRect.maxX - cornerRadius, y: borderRect.minY + cornerRadius),
radius: cornerRadius,
startAngle: -CGFloat.pi / 2,
endAngle: 0,
clockwise: true)
addLine(to: CGPoint(x: borderRect.maxX, y: borderRect.minY + cornerSizeV + cornerRadius))
// bottom-right
move(to: CGPoint(x: borderRect.maxX, y: borderRect.maxY - cornerSizeV - cornerRadius))
addLine(to: CGPoint(x: borderRect.maxX, y: borderRect.maxY - cornerRadius))
addArc(withCenter: CGPoint(x: borderRect.maxX - cornerRadius, y: borderRect.maxY - cornerRadius),
radius: cornerRadius,
startAngle: 0,
endAngle: CGFloat.pi / 2,
clockwise: true)
addLine(to: CGPoint(x: borderRect.maxX - cornerSizeH - cornerRadius, y: borderRect.maxY))
// bottom-left
move(to: CGPoint(x: borderRect.minX + cornerSizeH + cornerRadius, y: borderRect.maxY))
addLine(to: CGPoint(x: borderRect.minX + cornerRadius, y: borderRect.maxY))
addArc(withCenter: CGPoint(x: borderRect.minX + cornerRadius,
y: borderRect.maxY - cornerRadius),
radius: cornerRadius,
startAngle: CGFloat.pi / 2,
endAngle: CGFloat.pi,
clockwise: true)
addLine(to: CGPoint(x: borderRect.minX, y: borderRect.maxY - cornerSizeV - cornerRadius))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment