【发布时间】:2017-11-28 10:12:39
【问题描述】:
我正在视图中进行自定义绘图。它基本上只是画一个圆圈。我需要文本出现在那个圆圈的中间。我尝试使用 stringToDraw 方法,但它将我的文本与框架起始点对齐。有没有办法对齐?
override func draw(_ rect: CGRect) {
let arcCenter = CGPoint(x:bounds.size.width/2,y:bounds.size.height/2)
let halfSize:CGFloat = min( bounds.size.width/2, bounds.size.height/2)
let desiredLineWidth:CGFloat = 1 // your desired value
let circlePath = UIBezierPath(
arcCenter: arcCenter,
radius: CGFloat( halfSize - (1/2) ),
startAngle: CGFloat(0),
endAngle:CGFloat(CGFloat.pi * 2),
clockwise: true)
circlePath.lineWidth = desiredLineWidth
circlePath.stroke()
let stringToDraw = "Text" as NSString
let rectToDraw = bounds
stringToDraw.draw(in: rectToDraw)
}
【问题讨论】:
标签: ios swift text alignment drawing