Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1.  
  2. import UIKit
  3.  
  4. //
  5. // Collection of styles
  6. //
  7. private var styles:Dictionary<String,((view:UIView) -> Void)> = Dictionary()
  8.  
  9. class StyleCollection {
  10.  
  11. class func addStyle(style:String, paint:((view:UIView) -> Void)) -> Void {
  12. styles[style] = paint
  13. }
  14.  
  15. class func applyStyle(style:String?, view:UIView) -> Void {
  16.  
  17. // if no style defined
  18. if (style == nil) {
  19. return
  20. }
  21.  
  22. // let's apply!
  23. let styleList = style!.characters.split{$0 == " "}.map(String.init)
  24. for style:String in styleList {
  25. applyStyle(style, view:view)
  26. }
  27.  
  28. }
  29.  
  30. private class func applyStyle(style:String, view:UIView) -> Void {
  31. if let paint = styles[style] {
  32. paint(view:view)
  33. }
  34. }
  35.  
  36. }
  37.  
  38. //
  39. // Adds style property to UIView so styles can be applied
  40. //
  41. extension UIView {
  42.  
  43. @IBInspectable var style: String! {
  44. get {
  45. return nil
  46. }
  47. set(newValue) {
  48. StyleCollection.applyStyle(newValue, view:self)
  49. }
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement