From cd5401593a19f45d64bcf6bc73bd63478d379f4f Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Sun, 1 Sep 2013 13:06:11 -0400 Subject: [PATCH 1/2] Allow user to pass x,y and it's auto-turned into left, top for frames. --- lib/ProMotion/view/styling.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/ProMotion/view/styling.rb b/lib/ProMotion/view/styling.rb index 48325d7..1d97046 100644 --- a/lib/ProMotion/view/styling.rb +++ b/lib/ProMotion/view/styling.rb @@ -1,7 +1,7 @@ module ProMotion module Styling include Conversions - + def set_attributes(element, args = {}) args.each { |k, v| set_attribute(element, k, v) } element @@ -34,6 +34,8 @@ module ProMotion args[:resize].each { |r| attributes[:autoresizingMask] |= map_resize_symbol(r) } end + args[:left] = args.delete(:x) if args[:x] + args[:top] = args.delete(:y) if args[:y] if [:left, :top, :width, :height].select{ |a| args[a] && args[a] != :auto }.length == 4 attributes[:frame] = CGRectMake(args[:left], args[:top], args[:width], args[:height]) end @@ -64,7 +66,7 @@ module ProMotion end nil end - + def add(element, attrs = {}) add_to view_or_self, element, attrs end @@ -86,11 +88,11 @@ module ProMotion end element end - + def view_or_self self.respond_to?(:view) ? self.view : self end - + # These three color methods are stolen from BubbleWrap. def rgb_color(r,g,b) rgba_color(r,g,b,1) @@ -103,7 +105,7 @@ module ProMotion def hex_color(str) hex_color = str.gsub("#", "") - case hex_color.size + case hex_color.size when 3 colors = hex_color.scan(%r{[0-9A-Fa-f]}).map{ |el| (el * 2).to_i(16) } when 6 @@ -111,16 +113,16 @@ module ProMotion else raise ArgumentError end - + if colors.size == 3 rgb_color(colors[0], colors[1], colors[2]) else raise ArgumentError - end + end end - + protected - + def map_resize_symbol(symbol) @_resize_symbols ||= { left: UIViewAutoresizingFlexibleLeftMargin, @@ -128,10 +130,10 @@ module ProMotion top: UIViewAutoresizingFlexibleTopMargin, bottom: UIViewAutoresizingFlexibleBottomMargin, width: UIViewAutoresizingFlexibleWidth, - height: UIViewAutoresizingFlexibleHeight + height: UIViewAutoresizingFlexibleHeight } @_resize_symbols[symbol] || symbol end - + end end From 1980f5ac0a4e020c32e1a3eb83338b1387f693c7 Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Sun, 1 Sep 2013 13:08:40 -0400 Subject: [PATCH 2/2] Add passing test for setting frames with x/y instead of left/top --- spec/unit/view_helper_spec.rb | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/spec/unit/view_helper_spec.rb b/spec/unit/view_helper_spec.rb index b104c0e..7b25e16 100644 --- a/spec/unit/view_helper_spec.rb +++ b/spec/unit/view_helper_spec.rb @@ -79,7 +79,7 @@ describe "view helpers" do before do @dummy = UIView.alloc.initWithFrame CGRectZero @dummy.extend ProMotion::Styling - + @parent = UIView.alloc.initWithFrame(CGRectMake(0, 0, 320, 480)) @child = UIView.alloc.initWithFrame(CGRectZero) end @@ -89,11 +89,11 @@ describe "view helpers" do resize: [:left, :right, :top, :bottom, :width, :height] } - mask = UIViewAutoresizingFlexibleLeftMargin | - UIViewAutoresizingFlexibleRightMargin | - UIViewAutoresizingFlexibleTopMargin | - UIViewAutoresizingFlexibleBottomMargin | - UIViewAutoresizingFlexibleWidth | + mask = UIViewAutoresizingFlexibleLeftMargin | + UIViewAutoresizingFlexibleRightMargin | + UIViewAutoresizingFlexibleTopMargin | + UIViewAutoresizingFlexibleBottomMargin | + UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight @child.autoresizingMask.should == mask @@ -104,8 +104,8 @@ describe "view helpers" do resize: [:left, :right, :top] } - mask = UIViewAutoresizingFlexibleLeftMargin | - UIViewAutoresizingFlexibleRightMargin | + mask = UIViewAutoresizingFlexibleLeftMargin | + UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin @child.autoresizingMask.should == mask @@ -116,8 +116,8 @@ describe "view helpers" do resize: [:bottom, :width, :height] } - mask = UIViewAutoresizingFlexibleBottomMargin | - UIViewAutoresizingFlexibleWidth | + mask = UIViewAutoresizingFlexibleBottomMargin | + UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight @child.autoresizingMask.should == mask @@ -142,6 +142,17 @@ describe "view helpers" do @child.frame.should == CGRectMake(10, 20, 100, 50) end + it "Should create a frame with x & y" do + @dummy.set_easy_attributes @parent, @child, { + x: 10, + y: 20, + width: 100, + height: 50 + } + + @child.frame.should == CGRectMake(10, 20, 100, 50) + end + end end