2016-07-23 61 views
0

我使用的是Swift v2.2,我畫了一個矩形,如下圖所示,我打算填充它,然後在其中顯示一些白色文本。但是,我看到路徑已關閉,但矩形未填充。請幫助並感謝您的任何意見。Bezier關閉路徑沒有得到填充

代碼

class InstructionArrowBorderView: UIView { 

    var lineWidth: CGFloat = 1 {didSet { setNeedsDisplay() } } 
    var instructionRectPathColor: UIColor = UIColor(red:13/255.0, green:118/255.0, blue:255/255.0, alpha: 1.0) { didSet { setNeedsDisplay() } } 

    override func drawRect(rect: CGRect) { 

     let instructionRectPath = UIBezierPath() 
     instructionRectPath.moveToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50)) 
     instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.minY)) 
     instructionRectPath.moveToPoint(CGPoint(x: bounds.minX, y: bounds.minY)) 
     instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.minY)) 
     instructionRectPath.moveToPoint(CGPoint(x: bounds.maxX, y: bounds.minY)) 
     instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.maxY - 50)) 
     instructionRectPath.moveToPoint(CGPoint(x: bounds.maxX, y: bounds.maxY - 50)) 
     instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50)) 
     instructionRectPath.lineWidth = lineWidth 
     instructionRectPath.closePath() 
     instructionRectPath.fill() 
     instructionRectPathColor.set() 
     instructionRectPath.stroke() 
    } 
} 

佈局

  • 細胞

    • 視圖(如果貝塞爾曲線的路徑繪製)

      • 標籤(白字)

結果 enter image description here

+1

你不應該叫'instructionRectPathColor.set()''之前instructionRectPath.fill( )'? – beyowulf

+1

在調用'.fill()'之前,您沒有設置顏色,只需將它向下移動一行代碼即可,並且您應該看到它使用與邊框相同的顏色填充。現在只需填寫默認的顏色即可。 – NSGangster

回答

0

您將引入的不連續性的路徑。當你說addLineToPoint時,你也不需要說moveToPoint。這是防止您的路徑正確填充。您還需要設置顏色填充/描邊顏色,然後填充/描邊路徑。

drawRect代碼應該是:

let instructionRectPath = UIBezierPath() 
instructionRectPath.moveToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50)) 
instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.minY)) 
instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.minY)) 
instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.maxY - 50)) 
instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50)) 
instructionRectPath.closePath() 
instructionRectPath.lineWidth = lineWidth 
instructionRectPathColor.set() 
instructionRectPath.fill() 
instructionRectPath.stroke() 

這可能是更簡潔書面:

let instructionRectPath = UIBezierPath(rect: CGRect(x: bounds.minX, y: bounds.minY, width: bounds.maxX, height: bounds.maxY - 50)) 
instructionRectPath.lineWidth = lineWidth 
instructionRectPathColor.set() 
instructionRectPath.fill() 
instructionRectPath.stroke()