I want to create this badge or direction-type view in swift. I cannot use image because text can be long in other languages. I want to make it with UIView. I want to achieve this:
I managed to make it with sharp points with this code
class ArrowView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
private func commonInit() -> Void {
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0)) // Starting point: Top left
path.addLine(to: CGPoint(x: self.frame.width - self.frame.height/2, y: 0)) // Move to the top right before the arrow point
path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height/2)) // Move to the tip of the arrow
path.addLine(to: CGPoint(x: self.frame.width - self.frame.height/2, y: self.frame.height)) // Move to the bottom right before the arrow point
path.addLine(to: CGPoint(x: 0, y: self.frame.height)) // Move to the bottom left
path.close()
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.fillColor = UIColor.black.cgColor // Choose the color of the arrow
self.layer.addSublayer(shapeLayer)
}
override func layoutSubviews() {
self.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
self.commonInit()
}
}
How to make line joint rounded like the actual design?





Another option...
Work with
CGMutablePathand take advantage of tangent curves:Note: I used the same
@IBInspectableproperties to make it easy for you to compare with Rob's implementation.