文本不可见,因为
UIBezierPath
s取决于它们的绘制顺序。换句话说,,
尤比齐尔路径
super.draw(rect)
事实上,他画的是文本。但当你把它作为第一句话,它就会被画出来
第一
在上面
文本。要解决这个问题,你应该打电话
超级绘图(矩形)
override func draw(_ rect: CGRect) {
if bounds.height > bounds.width {
let y = (bounds.height - bounds.width) / 2
let path = UIBezierPath(ovalIn: CGRect(x: 0, y: y, width: bounds.width, height: bounds.width))
circleColor.setFill()
path.fill()
} else {
let x = (bounds.width - bounds.height) / 2
let path = UIBezierPath(ovalIn: CGRect(x: x, y: 0, width: bounds.height, height: bounds.height))
circleColor.setFill()
path.fill()
}
super.draw(rect) // <------- here!
}
或者,只是子类
UIView
draw(_:)
,并添加
UILabel
作为它的子视图。这种方法的优点是它不依赖于
super.draw(_:)