在UIButton中覆盖UILabel绘制rect

时间:2015-01-06 22:38:37

标签: ios swift

我试图覆盖UILabel" drawTextInRect"在UIButton中,我可以在文本周围创建一个边框。

我的代码与UILabel

完美配合
class LabelBorder: UILabel {

    override func drawTextInRect(rect: CGRect) {
        let shadowOffset = self.shadowOffset
        let textColor = self.textColor

        let c = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(c, 1)
        CGContextSetLineJoin(c, kCGLineJoinRound)

        CGContextSetTextDrawingMode(c, kCGTextStroke)
        self.textColor = UIColor.blackColor()
        self.alpha = 0.35
        super.drawTextInRect(rect)

        CGContextSetTextDrawingMode(c, kCGTextFill)
        self.textColor = textColor
        self.shadowOffset = CGSizeMake(0, 0)
        self.alpha = 1.0
        super.drawTextInRect(rect)

        self.shadowOffset = shadowOffset
    }

}

但我将UILabel更改为UIButton,因此我可以轻松检测到内部动作的触摸,但我仍然需要文本边框,所以我尝试将代码修改为此

class LabelBorderBtn: UIButton {


    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        super.drawRect(rect)


        let shadowOffset = self.titleLabel?.shadowOffset
        let textColor = self.titleLabel?.textColor

        let c = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(c, 1)
        CGContextSetLineJoin(c, kCGLineJoinRound)

        CGContextSetTextDrawingMode(c, kCGTextStroke)
        self.titleLabel?.textColor = UIColor.blackColor()
        self.titleLabel?.alpha = 0.35
        self.titleLabel?.drawTextInRect(rect)

        CGContextSetTextDrawingMode(c, kCGTextFill)
        self.titleLabel?.textColor = textColor
        self.titleLabel?.shadowOffset = CGSizeMake(0, 0)
        self.titleLabel?.alpha = 1.0
        self.titleLabel?.drawTextInRect(rect)

        self.titleLabel?.shadowOffset = shadowOffset!
    }
}

此代码不起作用,当然因为TitleLabel的绘图树时间,默认加上此代码中的两个。但是有一种方法可以在UIButton或其他任何想法中将UILabel子类化,以获得该标签中的边界?

我真的不想检测轻敲手势,因为这个按钮位于包含8种不同类型单元格的桌面视图中

由于

2 个答案:

答案 0 :(得分:0)

如果您希望按钮有边框,可以试试这个

var button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = UIColor.grayColor()
button.setTitle("Button", forState: UIControlState.Normal)
button.titleLabel?.layer.borderWidth = 1
button.titleLabel?.layer.borderColor = UIColor.redColor().CGColor

enter image description here

答案 1 :(得分:0)

我终于解决了它,基于我在问题上发布的LabelBorder类。解决方案是

class LabelBorderBtn: UIButton {


    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
        super.drawRect(rect)

        if let oldLabel = self.titleLabel {
            let newLabel = LabelBorder(frame: oldLabel.frame)
            newLabel.text = oldLabel.text
            newLabel.font = oldLabel.font
            newLabel.textColor = oldLabel.textColor

            oldLabel.removeFromSuperview()
            self.addSubview(newLabel)
        }
}

我知道它不是一个奇特的解决方案,但它可以工作,也适用于UITableView