4

How do I set, let's say a text color on a UILabel, to a color defined in one of my classes like:

func mainAppColor() -> UIColor {
    return UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}

or

let mainAppColor = UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)

in storyboard.

Rodrigo Ruiz
  • 4,248
  • 6
  • 43
  • 75

1 Answers1

1

If your method is declared in some other class like:

class TextColorClass {

    func mainAppColor() -> UIColor {
        return UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
    }
}

Then you can use it by make instance of that class this way:

let classInstance = TextColorClass()
yourLBL.textColor = classInstance.mainAppColor()

If it is declared like:

class TextColorClass {

    let mainAppColor = UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}

Then you can use it like:

let classInstance = TextColorClass()
yourLBL.textColor = classInstance.mainAppColor

If that method is declare in same class suppose in ViewController class then you can use it this way:

override func viewDidLoad() {
    super.viewDidLoad()

    yourLBL.textColor = mainAppColor()
}

func mainAppColor() -> UIColor {
    return UIColor(red: 0.2, green: 0.3, blue: 0.4, alpha: 1)
}

UPDATE:

If you want to set any property from storyboard you can use @IBInspectable as shown in below example code:

import UIKit

@IBDesignable
class PushButtonView: UIButton {

    @IBInspectable var fillColor: UIColor = UIColor.greenColor()
    @IBInspectable var isAddButton: Bool = true

    override func drawRect(rect: CGRect) {


    }

}

And assign this class to your button.like shown into below Image:

enter image description here

And if you go to the Attribute Inspector you can see custom property for that button.

enter image description here

Now you can set it as per your need.

For more Info refer this tutorial:

http://www.raywenderlich.com/90690/modern-core-graphics-with-swift-part-1

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • 1
    And the most obvious way to do something like this would be to declare it as a static, which makes it a class value, so you don't need to create an instance to use it. `static let mainColor =...` and then just TextColorClass.mainColor.. – David Berry Sep 26 '15 at 05:56
  • Thanks for your suggestion..:) @DavidBerry – Dharmesh Kheni Sep 26 '15 at 09:48
  • 5
    But that is not using storyboard... I know how to set a property in code, just not in storyboard using constants. – Rodrigo Ruiz Sep 26 '15 at 18:44