2016-07-23 45 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()