Reputation: 1701
I've created a UILabel through storyboard, but now I want to create another one programatically with the same properties of the first one
How can I duplicate or create a UILabel using the same style (font color, background, size, center, bold...) from other existing UILabel programatically.
Upvotes: 0
Views: 754
Reputation: 12023
You can create extension on UILabel
and add a method to style label.
extension UILabel {
func applyStyle() {
self.textColor = .black
self.textAlignment = .center
self.font = UIFont(name: "Helvetica", size: 14)
}
}
now you call myLabel.applyStyle()
to use same properties for different labels
Upvotes: 2
Reputation: 376
You could create a custom subclass of UILabel which has the properties you want it to have and then when you create you label just set its class to the custom class.
class CustomLabel: UILabel {
required init?(code aDecoder: NSCoder) {
super.init(code: aDecoder)
//your code goes here
self.textColor = UIColor.red
self.alpha = 0.5
}
}
So just add to your project a swift file and add the code above to it. There you can just add your properties.
I hope this helped
Upvotes: 1