diff --git a/README.md b/README.md index 58c7579..4f42f23 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Each object inside the JSON file should contain the name of the UIView as a key This would apply HelveticaNeue-Bold with size 20 to all the UIButtons except the ones contained inside the LoginView class in your app. -Custom classes must be namespaced by the name of the module they are contained in. e.g. `StyleKitDemo.SKTextField` +Custom classes can be namespaced by the name of the module they are contained in. e.g. `StyleKitDemo.SKTextField` ### Aliases diff --git a/StyleKit.xcodeproj/project.pbxproj b/StyleKit.xcodeproj/project.pbxproj index 1b8a6f9..59b8c99 100644 --- a/StyleKit.xcodeproj/project.pbxproj +++ b/StyleKit.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 059932DC1D59E0DF0085E522 /* SKTryCatch.m in Sources */ = {isa = PBXBuildFile; fileRef = 059932DB1D59E0DF0085E522 /* SKTryCatch.m */; }; 059932DD1D59E1950085E522 /* SKTryCatch.h in Headers */ = {isa = PBXBuildFile; fileRef = 059932D81D59E09D0085E522 /* SKTryCatch.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 3BD119FA1FB1B6EB000980B0 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 3BD119F91FB1B6EB000980B0 /* README.md */; }; 992723731D5B36D700B74CDD /* UIAppearance+Swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 992723711D5B36D700B74CDD /* UIAppearance+Swift.h */; settings = {ATTRIBUTES = (Public, ); }; }; 992723741D5B36D700B74CDD /* UIAppearance+Swift.m in Sources */ = {isa = PBXBuildFile; fileRef = 992723721D5B36D700B74CDD /* UIAppearance+Swift.m */; }; 996775991D5A2E8E005DA08A /* StyleParsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 996775981D5A2E8E005DA08A /* StyleParsable.swift */; }; @@ -56,6 +57,7 @@ /* Begin PBXFileReference section */ 059932D81D59E09D0085E522 /* SKTryCatch.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKTryCatch.h; sourceTree = ""; }; 059932DB1D59E0DF0085E522 /* SKTryCatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKTryCatch.m; sourceTree = ""; }; + 3BD119F91FB1B6EB000980B0 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 992723711D5B36D700B74CDD /* UIAppearance+Swift.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIAppearance+Swift.h"; sourceTree = ""; }; 992723721D5B36D700B74CDD /* UIAppearance+Swift.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIAppearance+Swift.m"; sourceTree = ""; }; 996775981D5A2E8E005DA08A /* StyleParsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StyleParsable.swift; sourceTree = ""; }; @@ -126,6 +128,7 @@ A10FD6901D53FA2600341EDD = { isa = PBXGroup; children = ( + 3BD119F91FB1B6EB000980B0 /* README.md */, A10FD69B1D53FA2600341EDD /* Products */, A10FD69C1D53FA2600341EDD /* StyleKit */, A10FD6A81D53FA2600341EDD /* StyleKitTests */, @@ -344,6 +347,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 3BD119FA1FB1B6EB000980B0 /* README.md in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/StyleKit/StyleKit.swift b/StyleKit/StyleKit.swift index 28b9fb5..0894486 100644 --- a/StyleKit/StyleKit.swift +++ b/StyleKit/StyleKit.swift @@ -4,7 +4,7 @@ public class StyleKit { let stylist: Stylist - public init?(fileUrl: URL, styleParser: StyleParsable? = nil, logLevel: SKLogLevel = .error) { + public init?(fileUrl: URL, styleParser: StyleParsable? = nil, moduleName: String? = nil, logLevel: SKLogLevel = .error) { let log = SKLogger.defaultInstance() log.setup(logLevel, showLogIdentifier: false, @@ -17,7 +17,7 @@ public class StyleKit { let fileLoader = FileLoader.init(fileUrl: fileUrl) if let data = fileLoader.load() { - self.stylist = Stylist.init(data: data, styleParser: styleParser) + self.stylist = Stylist.init(data: data, styleParser: styleParser, moduleName: moduleName) } else { return nil } diff --git a/StyleKit/Stylist.swift b/StyleKit/Stylist.swift index 15a7d49..9903876 100644 --- a/StyleKit/Stylist.swift +++ b/StyleKit/Stylist.swift @@ -8,10 +8,11 @@ class Stylist { let data: Style let aliases: Style let styleParser: StyleParsable + let moduleName: String? var currentComponent: AnyClass? var viewStack = [UIAppearanceContainer.Type]() - init(data: Style, styleParser: StyleParsable?) { + init(data: Style, styleParser: StyleParsable?, moduleName: String?) { self.styleParser = styleParser ?? StyleParser() var tmpAlias = Style() @@ -27,6 +28,7 @@ class Stylist { self.data = tmpData self.aliases = tmpAlias + self.moduleName = moduleName } func apply() { @@ -40,8 +42,8 @@ class Stylist { private func validateAndApply(_ data: Style) { for (key, value) in data { - if let value = value as? Style , NSClassFromString(key) != nil { - if selectCurrentComponent(key), let appearanceContainer = self.currentComponent! as? UIAppearanceContainer.Type { + if let value = value as? Style, let component = resolveComponent(from: key) { + if selectCurrentComponent(component), let appearanceContainer = self.currentComponent! as? UIAppearanceContainer.Type { viewStack.append(appearanceContainer) } validateAndApply(value) @@ -63,16 +65,25 @@ class Stylist { } } } - - private func selectCurrentComponent(_ name: String) -> Bool { - - SKLogger.debug("Switching to: \(name)") - - guard let currentComponent = NSClassFromString(name) else { - SKLogger.debug("Component \(name) cannot be selected") - return false + + private typealias Component = (klass: AnyClass, name: String) + + private func resolveComponent(from key: String) -> Component? { + let resolved: Component? + if let klass = NSClassFromString(key) { + resolved = (klass, key) + } else if let name = moduleName.flatMap({ "\($0).\(key)" }) + , let klass = NSClassFromString(name) { + resolved = (klass, name) + } else { + resolved = nil } - self.currentComponent = currentComponent + return resolved + } + + private func selectCurrentComponent(_ component: Component) -> Bool { + SKLogger.debug("Switching to: \(component.name)") + self.currentComponent = component.klass return true } diff --git a/StyleKitDemo/StyleKitDemo/AppDelegate.swift b/StyleKitDemo/StyleKitDemo/AppDelegate.swift index 90a6ab8..0729708 100644 --- a/StyleKitDemo/StyleKitDemo/AppDelegate.swift +++ b/StyleKitDemo/StyleKitDemo/AppDelegate.swift @@ -15,11 +15,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { //StyleKit(fileUrl: styleFile, styleParser: StyleParser())?.apply() // Uses default style parser - StyleKit(fileUrl: styleFile, logLevel: .debug)?.apply() + StyleKit(fileUrl: styleFile, moduleName: "StyleKitDemo", logLevel: .debug)?.apply() } - return true } diff --git a/StyleKitDemo/StyleKitDemo/Base.lproj/Main.storyboard b/StyleKitDemo/StyleKitDemo/Base.lproj/Main.storyboard index 517b961..2036acc 100644 --- a/StyleKitDemo/StyleKitDemo/Base.lproj/Main.storyboard +++ b/StyleKitDemo/StyleKitDemo/Base.lproj/Main.storyboard @@ -1,9 +1,14 @@ - - + + + + + - + + + @@ -11,21 +16,22 @@ - + - + - + - + - + - + - + - + - + - +