Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse colors and styles in iOS/xcode?

Android, WPF and mostly every platform I've been working with, has way to reuse and "centralize" ui resources like colors and styles in a single file.

in android it is posible to do like this:

in colors.xml file:

<color name="secondary_text_color">#ffcc67</color>

in any view:

<TextView text="some text" textColor="@colors/secondary_text_color" />

Is there something similar in iOS?

I'm not trying to replicate android in iOS, but I'm struggling with understanding what is (if there is any) ui reusing pattern that should be followed.

The only thing I've come across is to define Theme in code, and reuse it in code behind, is that the right way?

like image 699
daneejela Avatar asked Feb 03 '16 10:02

daneejela


3 Answers

Adding on to raki answer extensions of classes like UIColor or UIFont is what I have seen the most of and even though you have to do slightly more typing it is the equivalent in iOS.

extension UIFont {
class func myFont() -> UIFont {
    return UIFont(name: "HelveticaNeue", size: 22)!
}

class func otherFont() -> UIFont {
    return UIFont(name: "Arial", size: 22)!
}

}

Another thing that might be relevant coming from android is the equivalent of a strings file in iOS programming since you seem to be interested in reusing these styles. For strings:

You can add a strings file to your project

and then fill in strings you are going to use in your app

"ACTION_CELL_MENU" =                "Menu";

Then in your code elsewhere

label.text = NSLocalizedString("ACTION_CELL_MENU", comment: "")
like image 115
tnek316 Avatar answered Nov 07 '22 10:11

tnek316


You can do this by using any singleton class or UIColor extension. Following is an example UIColor extension.

import Foundation
import UIKit

extension UIColor
{
    class func someColor1() -> UIColor
    {
        return UIColor(red: 123.0/255.0, green: 162.0/255.0, blue: 157.0/255.0, alpha:1.0)
    }

    class func someColor2() -> UIColor
    {
        return UIColor(red: 154.0/255.0, green: 143.0/255.0, blue: 169.0/255.0, alpha:1.0)
    }
}

Later you can access the color like

textField.textColor = UIColor.someColor2()

Edit : You can do the same for styles too, using NSAttributedString

class StyleHelper : NSObject{

    class func getSecondaryTextWithString(textString:String) -> NSAttributedString
    {
        let secondaryTextStyleAttributes: [String : AnyObject] = [
            NSForegroundColorAttributeName: UIColor.greenColor(), NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue,NSFontAttributeName: UIFont.systemFontOfSize(14.0)]
        let secondaryTextStyleString = NSAttributedString(string: textString, attributes:secondaryTextStyleAttributes)
        return secondaryTextStyleString
    }
}

When you required the style, you will call like

someLabel.attributedText = StyleHelper.getSecondaryTextWithString("SomeText")
like image 26
Ravi Avatar answered Nov 07 '22 09:11

Ravi


Pretty old question, but you can actually add any color to Assets.xcassets, just call context menu on the list of resources, then "New Color Set" and done! You can use it with Interface Builder or programmatically.

like image 2
Vladimir Nitochkin Avatar answered Nov 07 '22 10:11

Vladimir Nitochkin