rb2030 rb2030 - 28 days ago 6
Swift Question

Updating a label with an integer from external source

I am currently trying to build a label using the help of

Snapkit
.

This label should replicate the data given to it from an external source in the UI.

This is the code I currently have.

I would like to incorporate an
IBOutlet
and change the
Int
that is represented in the circle.

I am trying to create this in my own custom class. Please see my code thus far below.

Thanks

import Foundation
import UIKit

class NumberInCircleView: UIView {

var numberLabel: UILabel!




override init(frame: CGRect) {
super.init(frame: frame)

self.setupViews()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

self.setupViews()
}



func setupViews() {



self.numberLabel = UILabel(frame: .zero)
self.addSubview(self.numberLabel)
self.numberLabel.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}

self.layer.cornerRadius = 30
self.backgroundColor = UIColor.red
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.black.cgColor
self.numberLabel.text = "1"
self.numberLabel.textAlignment = .center
self.numberLabel.textColor = UIColor.white
self.numberLabel.adjustsFontSizeToFitWidth = true
self.numberLabel.minimumScaleFactor = 0.5
}

}

Answer Source

You can set the integer by making it string.

var count:Int = 0
self.numberLabel.text = "\(count)"