UILabel subclass initialize with custom color
Asked Answered
C

2

10

My goal is to set the textColor of my custom UILabel subclass in my view controller. I have a UILabel subclass named CircleLabel. Here are the basics of it:

class CircleLabel: UILabel {

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}

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

override func drawRect(rect: CGRect) {
    self.layer.cornerRadius = self.bounds.width/2
    self.clipsToBounds = true
    super.drawRect(rect)
}

override func drawTextInRect(rect: CGRect) {
    self.textColor = UIColor.whiteColor()
    super.drawTextInRect(rect)
}

func setProperties(borderWidth: Float, borderColor: UIColor) {
    self.layer.borderWidth = CGFloat(borderWidth)
    self.layer.borderColor = borderColor.CGColor
}

}

As you can see, every CircleLabel I instantiate is defaulted to a textColor property of UIColor.whiteColor(), which works properly. In my view controller's viewDidLoad, I want to set my CircleLabel to have a dynamic textColor property. So something like this:

class myViewController: UIViewController {
    @IBOutlet weak var myCustomLabel: CircleLabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        myCustomLabel.textColor = UIColor.blackColor()
}

That doesn't work because textColor is set in the drawRect method of the UILabel subclass. What can I implement in my CircleLabel subclass (via a helper method like my setProperties helper method or some other way) that would allow me to set the textColor of my custom label in my view controller?

Chalfant answered 25/10, 2015 at 3:36 Comment(0)
M
28

Screenshot

You do not need to override drawRect in your case,just create the class like this

class CircleLabel: UILabel {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.commonInit()

    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }
    func commonInit(){
        self.layer.cornerRadius = self.bounds.width/2
        self.clipsToBounds = true
        self.textColor = UIColor.whiteColor()
        self.setProperties(1.0, borderColor:UIColor.blackColor())
    }
    func setProperties(borderWidth: Float, borderColor: UIColor) {
        self.layer.borderWidth = CGFloat(borderWidth)
        self.layer.borderColor = borderColor.CGColor
    }
}

Then

class ViewController: UIViewController {

    @IBOutlet weak var myCustomLabel: CircleLabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        myCustomLabel.textColor = UIColor.blackColor()
        // Do any additional setup after loading the view, typically from a nib.
    }

}
Miticide answered 25/10, 2015 at 4:44 Comment(2)
Would you take the same approach to override the "font" of a label that contains an Attributed Text? Like taking the current attributed text in the "common in" and overriding it with a mutable copy of it which contains a different font. (Just wondering if it would work since you have experience in it and i'm looking for this myself)Disparagement
I get Initializer does not override a designated initializer from its superclass when using override init on the UILabelMolt
L
1

1. Custom Label In swift

    class CustomLabel: UILabel {

    @IBInspectable var topInset: CGFloat = 5.0
    @IBInspectable var bottomInset: CGFloat = 5.0
    @IBInspectable var leftInset: CGFloat = 7.0
    @IBInspectable var rightInset: CGFloat = 7.0

    override func drawText(in rect: CGRect) {
        let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
        super.drawText(in: rect.inset(by: insets))
    }

    override var intrinsicContentSize: CGSize {
        let size = super.intrinsicContentSize
        return CGSize(width: size.width + leftInset + rightInset,
                      height: size.height + topInset + bottomInset)
    }
}
Lysippus answered 21/10, 2019 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.