UILabel subclass initialized with custom color

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

 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, each instance of CircleLabel I by default has the textColor property UIColor.whiteColor (), which works correctly. In my opinion, controller viewDidLoad, I want to set the dynamic property textColor for CircleLabel. So something like this:

 class myViewController: UIViewController { @IBOutlet weak var myCustomLabel: CircleLabel! override func viewDidLoad() { super.viewDidLoad() myCustomLabel.textColor = UIColor.blackColor() } 

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

+9
source share
2 answers

Screenshot

038R9.png

You do not need to override drawRect in your case, just create a class as follows

 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. } } 
+23
source

1. Custom label in fast

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

Source: https://habr.com/ru/post/1234455/


All Articles