【问题标题】:Aligning text inside custom drawing在自定义绘图中对齐文本
【发布时间】: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


【解决方案1】:

使用标签解决了我的问题:

override func draw(_ rect: CGRect) {
        let arcCenter = CGPoint(x:bounds.size.width/2,y:bounds.size.height/2)
        // drawRingFittingInsideView(arccenter: arcCenter)
        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)
        collisionPath = circlePath



        circlePath.lineWidth = desiredLineWidth
        circlePath.stroke()


        label.frame = bounds
        label.text = "AA"
        label.textAlignment = .center
        label.drawText(in: bounds)



    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-06
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多