2015-09-22 80 views
3

我想在运行时在TableViewCell的单元格中围绕标签绘制一个圆。Swift:围绕标签绘制一个圆

我可以弄清楚如何将它粗略地放在标签的周围,但我在将标签集中在标签周围时遇到了一些麻烦。

圆圈似乎正在绘制到右侧和朝向标签的中间。

这是我的代码到目前为止,我敢肯定,这将很容易有人轰炸。

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) 
} 

回答

1

呃,这是我的想法,愚蠢的错误。

X和Y是在处理贝塞尔路径时从中间而不是从左上角计算出来的。

因此,对于x和y的代码应该如下:

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

尝试这种情况:

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) 
} 

修改填充可变调整标签和圆之间的填充。 干杯!

12

您可以改为在标签的图层上使用圆角半径。您将标签设为正方形,然后将其图层的角半径设置为标签宽度/高度的一半,这将使其完美圆化。您将边框宽度设置为大于零的值,并将边框颜色设置为要使用的笔触颜色。

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 

它会是这个样子:

enter image description here

虽然是完整的代码是这样的单一视图控制器的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) 
    } 

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