Created
March 5, 2014 11:36
-
-
Save jacklynrose/9365600 to your computer and use it in GitHub Desktop.
SUPER basic clone of RMQ's stylesheet and selectors (no symbol based selection or fancy stylers, and other missing functionality like chaining)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppDelegate | |
def application(application, didFinishLaunchingWithOptions:launchOptions) | |
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) | |
@window.rootViewController = MyViewController.new | |
@window.makeKeyAndVisible | |
true | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyStylesheet | |
def bw_big(v) | |
v.backgroundColor = UIColor.blackColor | |
v.textColor = UIColor.whiteColor | |
v.font = UIFont.systemFontOfSize(36) | |
v.frame = [[20, 40], [280, 50]] | |
v.textAlignment = UITextAlignmentCenter | |
end | |
def white_bg(v) | |
v.backgroundColor = UIColor.whiteColor | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewController < UIViewController | |
def viewDidLoad | |
super | |
rmq.stylesheet = MyStylesheet.new # works absolutely anywhere | |
rmq(self.view).style(:white_bg) | |
self.view.addSubview(rmq(UILabel).style(:bw_big) do |v| | |
v.text = "Testing" | |
end) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def rmq(selector = nil) | |
RMQ.shared.selector = selector | |
RMQ.shared | |
end | |
end | |
class RMQ | |
attr_accessor :stylesheet, :selector | |
def self.shared | |
Dispatch.once { @shared ||= new } | |
@shared | |
end | |
def style(style_name) | |
begin | |
puts "\[RMQ ERROR] no stylesheet selected" unless self.stylesheet | |
view = self.selector | |
view = view.new if view.class == Class | |
self.stylesheet.public_send(style_name, view) | |
yield view if block_given? | |
view | |
rescue NoMethodError => e | |
if e.message =~ /.*#{style_name.to_s}.*/ | |
puts "\n[RMQ ERROR] style_name :#{style_name} doesn't exist for a #{view.class.name}. Add 'def #{style_name}(v)' to #{stylesheet.class.name} class\n\n" | |
else | |
raise e | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment