Swift - View Extensions

Aus Wikizone
Wechseln zu: Navigation, Suche

Links[Bearbeiten]

Swift (Programmiersprache)

Swift Extension[Bearbeiten]

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