0

I'm use extension of button for implements my own method to the UIButton, and the create a CATextLayer for add 1 more text in button, but where im call this method with different arguments (Text) previous text still behind new text and over and over. I need to repaint my button without stack texts (call method with new text and there gonna be only new text)

extension UIButton{

    func spordAcceptButAction(c_one: UIColor, c_two: UIColor, corner: Int, w: Int, number: Int, gradientLayer:CAGradientLayer){

        var textLayer: CATextLayer!

        let startFontSize: CGFloat = 60

        let startPosition: CGPoint = CGPoint(x: 0, y: 0)


        // Do any additional setup after loading the view, typically from a nib.
        // let gradientLayer = CAGradientLayer()
        textLayer = CATextLayer()
        textLayer.alignmentMode = CATextLayerAlignmentMode.left
        textLayer.anchorPoint = CGPoint(x: 0, y: 0)
        textLayer.bounds.size = CGSize(width: 100, height: 100)
        textLayer.contentsScale = UIScreen.main.scale
        textLayer.font = UIFont.systemFont(ofSize: startFontSize, weight: UIFont.Weight.semibold)
        textLayer.fontSize = startFontSize
        //  textLayer.backgroundColor = #colorLiteral(red: 0.521568656, green: 0.1098039225, blue: 0.05098039284, alpha: 1)
        textLayer.isWrapped = true
        textLayer.position = startPosition
        textLayer.string = String(number)

enter image description here

alexkent
  • 1,556
  • 11
  • 25
  • The problem you see is not that the layer fails to 'repaint', but rather with every call to your `spordAcceptButAction` method, a new CATextLayer is being instantiated and added as a sublayer of the button view's layer. I suggest you consider subclassing UIButton instead. – alexkent Apr 06 '20 at 13:35
  • @alexkent i solved this stpd problem. Sorry for my question, but i see u know swift(and something stuff). I Can we exchange our e-mails? I'm kind a new in swift, and sometimes i need someone who know more than me. – Evnika GK Apr 07 '20 at 17:03

0 Answers0