Swift - View Extensions

Aus Wikizone
Version vom 29. Dezember 2022, 19:46 Uhr von 134.3.86.14 (Diskussion) (Die Seite wurde neu angelegt: „The Swifty way is to create an extension. An extension can be used to modify any property of UILabel. For example, if you have basic labels: An extension o…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

The Swifty way is to create an extension.

An extension can be used to modify any property of UILabel.

For example, if you have basic labels:

An extension of UILabel could be created like this as an example:

    extension UILabel {
      func crazyShadow(color: UIColor = UIColor.red, size: CGSize = CGSize(width: 5, height: 5)) {
        self.shadowColor = color
        self.shadowOffset = size
      }
    }

//Then used like this

      @IBOutlet var testLabel1: UILabel!
      @IBOutlet var testLabel2: UILabel!
      @IBOutlet var testLabel3: UILabel!
      
      override func viewDidLoad() {
        super.viewDidLoad()
        testLabel1.crazyShadow()
        testLabel2.crazyShadow()
        testLabel3.crazyShadow(color: UIColor.cyan)
      }