motion-prime/views/view_styler.rb in motion-prime-0.4.2 vs motion-prime/views/view_styler.rb in motion-prime-0.4.3

- old
+ new

@@ -1,9 +1,10 @@ module MotionPrime class ViewStyler include FrameCalculatorMixin include HasStyles + include HasClassFactory attr_reader :view, :options def initialize(view, bounds = CGRectZero, options = {}) @options = Styles.extend_and_normalize_options options @@ -11,11 +12,13 @@ prepare_frame_for(bounds) if @options.delete(:calculate_frame) end def apply converted_options = convert_primitives_to_objects(options) - setValuesForKeysWithDictionary(converted_options) + converted_options.each do |key, value| + set_option(key.to_s, value) + end end def convert_primitives_to_objects(options) options.inject({}) do |result, (k, v)| v = STRUCTS_MAP[v.class].call(v) if STRUCTS_MAP.has_key?(v.class) @@ -37,11 +40,11 @@ mask |= UIViewAutoresizingFlexibleHeight if !options[:top].nil? && !options[:bottom].nil? options[:autoresizingMask] = mask end end - def setValue(value, forUndefinedKey: key) + def set_option(key, value) # return if value.nil? # ignore options return if key == 'section' && !view.respond_to?(:section=) return if key == 'size_to_fit' && view.is_a?(UILabel) return if (key == 'url' || key == 'default') && view.is_a?(UIImageView) @@ -54,19 +57,19 @@ # apply options if key.end_with?('title_color') view.setTitleColor value.uicolor, forState: UIControlStateNormal elsif key.end_with?('alignment') && value.is_a?(Symbol) - view.setValue value.uitextalignment, forKey: key.camelize + view.setValue value.uitextalignment, forKey: camelize_factory(key) elsif key.end_with?('line_break_mode') && value.is_a?(Symbol) - view.setValue value.uilinebreakmode, forKey: key.camelize + view.setValue value.uilinebreakmode, forKey: camelize_factory(key) elsif key.end_with?('title_shadow_color') view.setTitleShadowColor value.uicolor, forState: UIControlStateNormal elsif key.end_with?('color') color = value.try(:uicolor) color = color.cgcolor if view.is_a?(CALayer) - view.send :"#{key.camelize(:lower)}=", color + view.send :"#{low_camelize_factory(key)}=", color elsif key.end_with?('background_image') if view.is_a?(UIButton) view.setBackgroundImage value.uiimage, forState: UIControlStateNormal elsif view.is_a?(UISearchBar) && key == 'search_field_background_image' view.setSearchFieldBackgroundImage value.uiimage, forState: UIControlStateNormal @@ -77,14 +80,14 @@ if view.is_a?(UITableView) bg_view = UIView.alloc.initWithFrame(view.bounds) bg_view.backgroundColor = value[:color].uicolor view.backgroundView = bg_view else - view.setValue value, forKey: key.camelize(:lower) + view.setValue value, forKey: low_camelize_factory(key) end elsif key.end_with?('image') - view.setValue value.uiimage, forKey: key.camelize + view.setValue value.uiimage, forKey: camelize_factory(key) elsif key.end_with?('_content_inset') current_inset = view.contentInset current_inset.send("#{key.partition('_').first}=", value) view.contentInset = current_inset elsif key == 'autocapitalization' @@ -142,12 +145,12 @@ view.separatorInset = UIEdgeInsetsMake(0, 320, 0, 0) elsif value.is_a?(Array) && value.count == 2 view.separatorInset = UIEdgeInsetsMake(0, value.first, 0, value.last) end elsif value.is_a?(Hash) - self.class.new(view.send(key.camelize(:lower).to_sym), nil, value.merge(parent_frame: options[:frame] || options[:parent_frame])).apply + self.class.new(view.send(low_camelize_factory(key).to_sym), nil, value.merge(parent_frame: options[:frame] || options[:parent_frame])).apply else - view.setValue value, forKey: key.camelize(:lower) + view.setValue value, forKey: low_camelize_factory(key) end end STRUCTS_MAP = { CGAffineTransform => Proc.new {|v| NSValue.valueWithCGAffineTransform(v) }, \ No newline at end of file