motion-prime/views/view_styler.rb in motion-prime-0.9.4 vs motion-prime/views/view_styler.rb in motion-prime-0.9.5
- old
+ new
@@ -28,11 +28,11 @@
result
end
end
def prepare_frame_for(bounds)
- options[:frame] = calculate_frome_for(bounds, options.merge(test: view.is_a?(UITextView)))
+ options[:frame] = calculate_frame_for(bounds, options.merge(test: view.is_a?(UITextView)))
if options.slice(:width, :height, :right, :bottom, :height_to_fit).values.any?
mask = UIViewAutoresizingNone
mask |= UIViewAutoresizingFlexibleTopMargin if options[:top].nil?
mask |= UIViewAutoresizingFlexibleLeftMargin if options[:left].nil?
mask |= UIViewAutoresizingFlexibleBottomMargin if options[:bottom].nil?
@@ -82,94 +82,154 @@
end
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 %w[url default draw_in_rect].include?(key.to_s) && view.is_a?(UIImageView)
- return if %w[
- styles has_drawn_content
- width height top right bottom left value_type
- max_width max_outer_width min_width min_outer_width
- max_height max_outer_height min_height min_outer_width
- height_to_fit container parent_frame].include? key.to_s
+ return if ignore_option?(key)
# 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: camelize_factory(key)
- elsif key.end_with?('line_break_mode') && value.is_a?(Symbol)
- 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 :"#{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
- else
- view.setBackgroundColor value.uiimage.uicolor
+ result ||= set_color_options(key, value)
+ result ||= set_image_options(key, value)
+ result ||= set_text_options(key, value)
+ result ||= set_inset_options(key, value)
+ result ||= set_layer_options(key, value)
+ result ||= set_other_options(key, value)
+ result ||= set_hash_options(key, value)
+
+ unless result
+ view.setValue value, forKey: low_camelize_factory(key)
+ end
+ end
+
+ protected
+ def set_color_options(key, value)
+ if key.end_with?('color') && view.is_a?(UIControl)
+ view.send :"set#{camelize_factory(key)}:forState", value.uicolor, UIControlStateNormal
+ true
+ elsif key.end_with?('color')
+ color = value.try(:uicolor)
+ color = color.cgcolor if view.is_a?(CALayer)
+ view.send :"#{low_camelize_factory(key)}=", color
+ true
+ elsif key == 'gradient'
+ gradient = prepare_gradient(value)
+ view.layer.insertSublayer(gradient, atIndex: 0)
+ true
end
- elsif key.end_with?('background_view')
- 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: low_camelize_factory(key)
+ end
+
+ def set_image_options(key, value)
+ if key.end_with?('background_image')
+ if view.is_a?(UIControl) || view.is_a?(UISearchBar)
+ view.send :"set#{camelize_factory(key)}:forState", value.uiimage, UIControlStateNormal
+ else
+ view.setBackgroundColor value.uiimage.uicolor
+ end
+ true
+ elsif key.end_with?('background_view')
+ 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: low_camelize_factory(key)
+ end
+ true
+ elsif key.end_with?('image')
+ view.setValue value.uiimage, forKey: camelize_factory(key)
+ true
end
- elsif key.end_with?('image')
- 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'
- view.autocapitalizationType = UITextAutocapitalizationTypeNone if value === false
- elsif key == 'keyboard_type'
- view.setKeyboardType value.uikeyboardtype
- elsif key == 'rounded_corners'
- radius = value[:radius].to_f
- corner_consts = {top_left: UIRectCornerTopLeft, bottom_left: UIRectCornerBottomLeft, bottom_right: UIRectCornerBottomRight, top_right: UIRectCornerTopRight}
- corners = value[:corners].inject(0) { |result, corner| result|corner_consts[corner] }
- size = options[:parent_frame].size
- bounds = CGRectMake(0, 0, size.width, size.height)
- mask_path = UIBezierPath.bezierPathWithRoundedRect(bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(radius, radius))
- mask_layer = CAShapeLayer.layer
- mask_layer.frame = bounds
- mask_layer.path = mask_path.CGPath
- view.mask = mask_layer
- elsif key == 'attributed_text'
- if view.is_a?(UIButton)
- view.setAttributedTitle value, forState: UIControlStateNormal
- else
- view.attributedText = value
+ end
+
+ def set_text_options(key, value)
+ if key.end_with?('alignment') && value.is_a?(Symbol)
+ view.setValue value.uitextalignment, forKey: camelize_factory(key)
+ true
+ elsif key.end_with?('line_break_mode') && value.is_a?(Symbol)
+ view.setValue value.uilinebreakmode, forKey: camelize_factory(key)
+ true
+ elsif key == 'autocapitalization'
+ view.autocapitalizationType = UITextAutocapitalizationTypeNone if value === false
+ true
+ elsif key == 'attributed_text'
+ if view.is_a?(UIButton)
+ view.setAttributedTitle value, forState: UIControlStateNormal
+ else
+ view.attributedText = value
+ end
+ true
end
- elsif key == 'gradient'
- gradient = prepare_gradient(value)
- view.layer.insertSublayer(gradient, atIndex: 0)
- elsif key == 'selection_style' && view.is_a?(UITableViewCell) && value.is_a?(Symbol)
- view.setSelectionStyle value.uitablecellselectionstyle
- elsif key == 'estimated_cell_height' && view.is_a?(UITableView)
- view.setEstimatedRowHeight value
- elsif key == 'separator_inset' && (view.is_a?(UITableViewCell) || view.is_a?(UITableView))
- if value.to_s == 'none'
- 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
+
+ def set_inset_options(key, value)
+ if key.end_with?('_content_inset')
+ current_inset = view.contentInset
+ current_inset.send("#{key.partition('_').first}=", value)
+ view.contentInset = current_inset
+ true
+ elsif key.end_with?('inset')
+ inset = if value.to_s == 'none'
+ UIEdgeInsetsMake(0, 320, 0, 0)
+ elsif value.is_a?(Array) && value.count == 2
+ UIEdgeInsetsMake(0, value.first, 0, value.last)
+ elsif value.is_a?(Array) && value.count == 4
+ UIEdgeInsetsMake(value[0], value[1], value[2], value[3])
+ end
+ view.send(:"#{low_camelize_factory(key)}=", inset) if inset
+ true
end
- elsif value.is_a?(Hash)
- 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: low_camelize_factory(key)
end
- end
+
+ def set_layer_options(key, value)
+ if key == 'rounded_corners'
+ radius = value[:radius].to_f
+ corner_consts = {top_left: UIRectCornerTopLeft, bottom_left: UIRectCornerBottomLeft, bottom_right: UIRectCornerBottomRight, top_right: UIRectCornerTopRight}
+ corners = value[:corners].inject(0) { |result, corner| result|corner_consts[corner] }
+ size = options[:parent_frame].size
+ bounds = CGRectMake(0, 0, size.width, size.height)
+ mask_path = UIBezierPath.bezierPathWithRoundedRect(bounds, byRoundingCorners: corners, cornerRadii: CGSizeMake(radius, radius))
+ mask_layer = CAShapeLayer.layer
+ mask_layer.frame = bounds
+ mask_layer.path = mask_path.CGPath
+ view.mask = mask_layer
+ true
+ end
+ end
+
+ def set_hash_options(key, value)
+ if value.is_a?(Hash)
+ self.class.new(
+ view.send(low_camelize_factory(key).to_sym), nil,
+ value.merge(parent_frame: options[:frame] || options[:parent_frame])
+ ).apply
+ true
+ end
+ end
+
+ def set_other_options(key, value)
+ if key == 'keyboard_type'
+ view.setKeyboardType value.uikeyboardtype
+ true
+ elsif key == 'selection_style' && view.is_a?(UITableViewCell) && value.is_a?(Symbol)
+ view.setSelectionStyle value.uitablecellselectionstyle
+ true
+ elsif key == 'estimated_cell_height' && view.is_a?(UITableView)
+ view.setEstimatedRowHeight value
+ true
+ end
+ end
+
+ def ignore_option?(key)
+ (key == 'section' && !view.respond_to?(:section=)) ||
+ (key == 'size_to_fit' && view.is_a?(UILabel)) ||
+ (%w[url default draw_in_rect].include?(key.to_s) && view.is_a?(UIImageView)) ||
+ %w[
+ styles has_drawn_content value_type height_to_fit container parent_frame
+ width height top right bottom left
+ max_width max_outer_width min_width min_outer_width
+ max_height max_outer_height min_height min_outer_width
+ ].include?(key.to_s)
+ end
STRUCTS_MAP = {
CGAffineTransform => Proc.new {|v| NSValue.valueWithCGAffineTransform(v) },
CGPoint => Proc.new {|v| NSValue.valueWithCGPoint(v) },
CGRect => Proc.new {|v| NSValue.valueWithCGRect(v) },
\ No newline at end of file