Skip to content

Commit

Permalink
Merge pull request #3 from vadymmarkov/feature/string-convertible
Browse files Browse the repository at this point in the history
Feature: string convertible
  • Loading branch information
vadymmarkov committed Apr 21, 2016
2 parents 51ac39a + dc09360 commit 3e2bf87
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Fashion.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "Fashion"
s.summary = "Fashion accessories and beauty tools to share and reuse UI styles in a Swifty way"
s.version = "1.0.0"
s.version = "1.1.0"
s.homepage = "https://github.com/vadymmarkov/Fashion"
s.license = 'MIT'
s.author = { "Vadym Markov" => "[email protected]" }
Expand Down
6 changes: 6 additions & 0 deletions Fashion.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
D5C629851C3A893F007F7B7C /* NSView+Styleable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C629841C3A893F007F7B7C /* NSView+Styleable.swift */; };
D5C629871C3A89A8007F7B7C /* Style.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C629861C3A89A8007F7B7C /* Style.swift */; };
D5C629881C3A89A8007F7B7C /* Style.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C629861C3A89A8007F7B7C /* Style.swift */; };
D5F2E6A21CC98D5200D180B9 /* StringConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F2E6A11CC98D5200D180B9 /* StringConvertible.swift */; };
D5F2E6A31CC98D5200D180B9 /* StringConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F2E6A11CC98D5200D180B9 /* StringConvertible.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -93,6 +95,7 @@
D5C629821C3A892A007F7B7C /* UIView+Styleable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Styleable.swift"; sourceTree = "<group>"; };
D5C629841C3A893F007F7B7C /* NSView+Styleable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSView+Styleable.swift"; sourceTree = "<group>"; };
D5C629861C3A89A8007F7B7C /* Style.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Style.swift; sourceTree = "<group>"; };
D5F2E6A11CC98D5200D180B9 /* StringConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringConvertible.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -258,6 +261,7 @@
D52158671C948CE2004EE4BE /* Stylesheet.swift */,
D521586F1C94A444004EE4BE /* Stylist.swift */,
D52158761C95B369004EE4BE /* Fashion.swift */,
D5F2E6A11CC98D5200D180B9 /* StringConvertible.swift */,
);
path = Shared;
sourceTree = "<group>";
Expand Down Expand Up @@ -491,6 +495,7 @@
D5C629871C3A89A8007F7B7C /* Style.swift in Sources */,
D52158701C94A444004EE4BE /* Stylist.swift in Sources */,
D52158741C95B117004EE4BE /* StyleManaging.swift in Sources */,
D5F2E6A21CC98D5200D180B9 /* StringConvertible.swift in Sources */,
D52158771C95B369004EE4BE /* Fashion.swift in Sources */,
D54B580F1C95DCF90029F7BC /* UIView+SharedStyles.swift in Sources */,
D52158681C948CE2004EE4BE /* Stylesheet.swift in Sources */,
Expand Down Expand Up @@ -525,6 +530,7 @@
D5C629881C3A89A8007F7B7C /* Style.swift in Sources */,
D52158781C95B369004EE4BE /* Fashion.swift in Sources */,
D52158691C948CE2004EE4BE /* Stylesheet.swift in Sources */,
D5F2E6A31CC98D5200D180B9 /* StringConvertible.swift in Sources */,
D521588E1C95CBFC004EE4BE /* Styleable.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ type.
**Define styles in a stylesheet**

```swift
enum Style: String, StringConvertible {
case CustomButton

var string: String {
return rawValue
}
}

struct MainStylesheet: Stylesheet {

func define() {
Expand All @@ -43,7 +51,8 @@ struct MainStylesheet: Stylesheet {
label.adjustsFontSizeToFitWidth = true
}

register("custom-button") { (button: UIButton) in
// register("custom-button") { (button: UIButton) in
register(Style.CustomButton) { (button: UIButton) in
button.backgroundColor = UIColor.redColor()
button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
}
Expand All @@ -60,7 +69,7 @@ Fashion.register([MainStylesheet()])

```swift
let button = UIButton() // let button = UIButton(styles: "custom-button")
button.styles = "custom-button" // backgroundColor => UIColor.redColor()
button.stylize(Style.CustomButton) // backgroundColor => UIColor.redColor()

let label = UILabel()
addSubview(label) // textColor => UIColor.blueColor()
Expand Down Expand Up @@ -175,15 +184,15 @@ let label = UILabel(styles: "content-view cool-label")
```

```swift
// The initialized also accepts CustomStringConvertible, so something other
// The initialized also accepts StringConvertible, so something other
// than magic String could also be used

enum Style: String, CustomStringConvertible {
enum Style: String, StringConvertible {
case CustomButton
case ContentView
case CoolLabel

var description: String {
var string: String {
return rawValue
}
}
Expand All @@ -200,11 +209,11 @@ let label = UILabel(styles: [Style.ContentView, Style.CoolLabel])
```swift
let label = UILabel()

// StringConvertible
label.stylize(Style.ContentView, Style.CoolLabel)

// String
label.stylize("content-view", "cool-label")

// CustomStringConvertible
label.stylize(Style.ContentView, Style.CoolLabel)
```

**With `@IBInspectable` property `styles`**
Expand Down
10 changes: 10 additions & 0 deletions Sources/Shared/StringConvertible.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public protocol StringConvertible {
var string: String { get }
}

extension String: StringConvertible {

public var string: String {
return self
}
}
4 changes: 2 additions & 2 deletions Sources/Shared/StyleManaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public protocol StyleManaging {
- Parameter name: The name of the style you can apply to your view afterwards.
- Parameter stylization: Closure where you can apply styles.
*/
func register<T: Styleable>(name: String, stylization: T -> Void)
func register<T: Styleable>(name: StringConvertible, stylization: T -> Void)

/**
Unregisters stylization closure with the specified name.

- Parameter name: The name of the style you can apply to your view afterwards.
*/
func unregister(name: String)
func unregister(name: StringConvertible)

/**
Registers stylization closure on type label.
Expand Down
4 changes: 2 additions & 2 deletions Sources/Shared/Stylesheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public extension Stylesheet {
- Parameter name: The name of the style you can apply to your view afterwards.
- Parameter stylization: Closure where you can apply styles.
*/
public func register<T: Styleable>(name: String, stylization: T -> Void) {
public func register<T: Styleable>(name: StringConvertible, stylization: T -> Void) {
Stylist.master.register(name, stylization: stylization)
}

Expand All @@ -27,7 +27,7 @@ public extension Stylesheet {

- Parameter name: The name of the style you want to unregister.
*/
public func unregister(name: String) {
public func unregister(name: StringConvertible) {
Stylist.master.unregister(name)
}

Expand Down
12 changes: 6 additions & 6 deletions Sources/Shared/Stylist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class Stylist {
var styles: [String: Stylization] = [:]

// MARK: - Initialization

public init() {}

// MARK: - Stylization

/**
Expand Down Expand Up @@ -77,19 +77,19 @@ extension Stylist: StyleManaging {
- Parameter name: The name of the style you can apply to your view afterwards.
- Parameter stylization: Closure where you can apply styles.
*/
public func register<T: Styleable>(name: String, stylization: T -> Void) {
public func register<T: Styleable>(name: StringConvertible, stylization: T -> Void) {
let style = Style(process: stylization)

styles[name] = style.applyTo
styles[name.string] = style.applyTo
}

/**
Unregisters stylization closure with the specified name.

- Parameter name: The name of the style you want to unregister.
*/
public func unregister(name: String) {
styles.removeValueForKey(name)
public func unregister(name: StringConvertible) {
styles.removeValueForKey(name.string)
}

/**
Expand Down
22 changes: 7 additions & 15 deletions Sources/iOS/UIView+Styleable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ extension UIView {
static var Style = "fashion_StyleAssociatedKey"
}

public convenience init(frame: CGRect = CGRectZero, styles: String) {
public convenience init(frame: CGRect = CGRectZero, styles: [StringConvertible]) {
self.init(frame: frame)
self.styles = styles
self.styles = styles.map { $0.string } .joinWithSeparator(" ")
}

public convenience init(frame: CGRect = CGRectZero, styles: [CustomStringConvertible]) {
self.init(frame: frame)
stylize(styles)
}

public convenience init(frame: CGRect = CGRectZero, styles: CustomStringConvertible) {

public convenience init(frame: CGRect = CGRectZero, styles: StringConvertible) {
self.init(frame: frame)
stylize(styles)
}
Expand All @@ -26,12 +21,9 @@ extension UIView {

- Parameter styles: Set of style names.
*/
public func stylize(styles: String...) {
self.styles = styles.joinWithSeparator(" ")
}

public func stylize(styles: CustomStringConvertible...) {
self.styles = styles.map { String($0) } .joinWithSeparator(" ")

public func stylize(styles: StringConvertible...) {
self.styles = styles.map { $0.string } .joinWithSeparator(" ")
}

/**
Expand Down

0 comments on commit 3e2bf87

Please sign in to comment.