简体   繁体   English

Swift:在标签周围画一个圆圈

[英]Swift: Draw a Circle around a Label

I'm trying to draw a circle around a label at runtime in a TableViewCell's cell. 我试图在运行时在TableViewCell的单元格中围绕标签绘制一个圆圈。

I can figure out how to get it roughly around the label, but I'm having some trouble centring it exactly around the label. 我可以弄清楚如何在标签周围大致得到它,但是我很难将它集中在标签周围。

The circle seems to be drawing just to the right and towards the middle of the label. 圆圈似乎正在向标签的右侧和中间绘制。

Here's my code so far, I'm sure this will be easy for someone to bang out. 到目前为止,这是我的代码,我相信这对于某人来说很容易。

func drawCircle() {
    let x = countLabel.layer.position.x - (countLabel.frame.width)
    let y = countLabel.layer.position.y - (countLabel.frame.height / 2)
    let circlePath = UIBezierPath(roundedRect: CGRectMake(x, y, countLabel.frame.height, countLabel.frame.height), cornerRadius: countLabel.frame.height / 2).CGPath

    let circleShape = CAShapeLayer()
    circleShape.path = circlePath
    circleShape.lineWidth = 3
    circleShape.strokeColor = UIColor.whiteColor().CGColor
    circleShape.fillColor = UIColor.clearColor().CGColor

    self.layer.addSublayer(circleShape)
}

You can instead just use corner radius on your label's layer. 您只需在标签的图层上使用圆角半径即可。 You make the label square and then set its layer's corner radius to half the width/height of the label which will make it perfectly round. 您将标签制作为方形,然后将其图层的圆角半径设置为标签宽度/高度的一半,这将使其完美呈圆形。 The you set the border width to something greater than zero and the border color to the stroke color you want to use. 您将边框宽度设置为大于零的值,并将边框颜色设置为要使用的笔触颜色。

let size:CGFloat = 35.0 // 35.0 chosen arbitrarily

countLabel.bounds = CGRectMake(0.0, 0.0, size, size) 
countLabel.layer.cornerRadius = size / 2
countLabel.layer.borderWidth = 3.0
countLabel.layer.backgroundColor = UIColor.clearColor().CGColor
countLabel.layer.borderColor = UIColor.greenColor().CGColor

It will look something like this: 它看起来像这样:

在此输入图像描述

Although the full code for that goes like this in a single view controller iPad template project: 虽然在单个视图控制器iPad模板项目中完整的代码是这样的:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let size:CGFloat = 35.0 // 35.0 chosen arbitrarily

        let countLabel = UILabel()
        countLabel.text = "5"
        countLabel.textColor = .greenColor()
        countLabel.textAlignment = .Center
        countLabel.font = UIFont.systemFontOfSize(14.0)
        countLabel.bounds = CGRectMake(0.0, 0.0, size, size)
        countLabel.layer.cornerRadius = size / 2
        countLabel.layer.borderWidth = 3.0
        countLabel.layer.backgroundColor = UIColor.clearColor().CGColor
        countLabel.layer.borderColor = UIColor.greenColor().CGColor

        countLabel.center = CGPointMake(200.0, 200.0)

        self.view.addSubview(countLabel)
    }

}
countLabel.layer.cornerRadius = 0.5 * countLabel.bounds.size.width 

Ugh, it was what I thought, silly mistake. 呃,这是我的想法,愚蠢的错误。

X and Y are calculated from the middle instead of from the top left when dealing with BezierPath's. 在处理BezierPath时,X和Y是从中间而不是从左上角计算的。

So the code for x and y should be as follows: 所以x和y的代码应该如下:

let x = countLabel.layer.position.x - (countLabel.frame.height / 2)
let y = countLabel.layer.position.y - (countLabel.frame.height / 2)

Try this: 尝试这个:

func drawCircle() {
    var padding : CGFloat = 8

    let x = countLabel.layer.position.x - (countLabel.frame.width / 2)
    let y = countLabel.layer.position.y  - (countLabel.frame.width / 2)
    let circlePath = UIBezierPath(roundedRect: CGRectMake(x - padding, y - padding, countLabel.frame.width + (2 * padding), countLabel.frame.width + (2 * padding)), cornerRadius: (countLabel.frame.width + (2 * padding)) / 2).CGPath

    let circleShape = CAShapeLayer()
    circleShape.path = circlePath
    circleShape.lineWidth = 3
    circleShape.strokeColor = UIColor.greenColor().CGColor
    circleShape.fillColor = UIColor.clearColor().CGColor

    self.view.layer.addSublayer(circleShape)
}

Modify padding variable to adjust the padding between the label and the circle. 修改填充变量以调整标签和圆圈之间的填充。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM