How to change the color of a UIBezierPath in Swift?

后端 未结 2 1878
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 06:15

I have an instance of UIBezierPath and I want to change the color of the stroke to something other than black. Does anyone know how to do this in Swift?

2条回答
  •  时光取名叫无心
    2021-02-07 06:51

    Let's assume you want to use the color red instead to stroke a rounded rectangle; This is how you do it in Swift 3 :

        // Drawing the border of the rounded rectangle:
        let redColor = UIColor.red
        redColor.setStroke() // Stroke subsequent views with a red color
        let roundedRectagle = CGRect(x: 0,y: 0, width: 90,height: 20)
        let rectangleBorderPath = UIBezierPath(roundedRect: roundedRectangle,cornerRadius: 5)
        roundedRectangle.borderWidth = 1
        roundedRectangle.stroke() // Apply the red color stroke on this view
    

    The second and last lines of the above code are important in answering your question.I hope this answer is helpful.

提交回复
热议问题