module ProMotion::Styling

Public Instance Methods

add(element, attrs = {}) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 67
def add(element, attrs = {})
  add_to view_or_self, element, attrs
end
add_to(parent_element, elements, attrs = {}) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 75
def add_to(parent_element, elements, attrs = {})
  attrs = get_attributes_from_symbol(attrs)
  Array(elements).each do |element|
    parent_element.addSubview element
    set_attributes(element, attrs) if attrs && attrs.length > 0
    element.send(:on_load) if element.respond_to?(:on_load)
  end
  elements
end
camelize(str) click to toggle source

Turns a snake_case string into a camelCase string.

# File lib/ProMotion/styling/styling.rb, line 115
def camelize(str)
  str.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
end
closest_parent(type, this_view = nil) click to toggle source

iterate up the view hierarchy to find the parent element of “type” containing this view

# File lib/ProMotion/styling/styling.rb, line 58
def closest_parent(type, this_view = nil)
  this_view ||= view_or_self.superview
  while this_view != nil do
    return this_view if this_view.is_a? type
    this_view = this_view.superview
  end
  nil
end
content_height(view) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 48
def content_height(view)
  content_max(view, :height)
end
content_max(view, mode = :height) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 36
def content_max(view, mode = :height)
  view.subviews.map do |sub_view|
    if sub_view.isHidden
      0
    elsif mode == :height
      sub_view.frame.origin.y + sub_view.frame.size.height
    else
      sub_view.frame.origin.x + sub_view.frame.size.width
    end
  end.max.to_f
end
content_width(view) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 52
def content_width(view)
  content_max(view, :width)
end
hex_color(str) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 99
def hex_color(str)
  hex_color = str.gsub("#", "")
  case hex_color.size
  when 3
    colors = hex_color.scan(%r{[0-9A-Fa-f]}).map{ |el| (el * 2).to_i(16) }
  when 6
    colors = hex_color.scan(%r<[0-9A-Fa-f]{2}>).map{ |el| el.to_i(16) }
  else
    raise ArgumentError
  end

  raise ArgumentError unless colors.size == 3
  rgb_color(colors[0], colors[1], colors[2])
end
remove(elements) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 71
def remove(elements)
  Array(elements).each(&:removeFromSuperview)
end
rgb_color(r,g,b) click to toggle source

These three color methods are stolen from BubbleWrap.

# File lib/ProMotion/styling/styling.rb, line 90
def rgb_color(r,g,b)
  rgba_color(r,g,b,1)
end
rgba_color(r,g,b,a) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 94
def rgba_color(r,g,b,a)
  r,g,b = [r,g,b].map { |i| i / 255.0}
  UIColor.colorWithRed(r, green: g, blue:b, alpha:a)
end
set_attribute(element, k, v) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 13
def set_attribute(element, k, v)
  return unless element

  if !element.is_a?(CALayer) && v.is_a?(Hash) && element.respond_to?("#{k}=")
    element.send("#{k}=", v)
  elsif v.is_a?(Hash) && element.respond_to?(k)
    sub_element = element.send(k)
    set_attributes(sub_element, v) if sub_element
  elsif element.respond_to?("#{k}=")
    element.send("#{k}=", v)
  elsif v.is_a?(Array) && element.respond_to?("#{k}") && element.method("#{k}").arity == v.length
    element.send("#{k}", *v)
  elsif k.to_s.include?("_") # Snake case?
    set_attribute(element, camelize(k), v)
  else # Warn
    mp "set_attribute: #{element.inspect} does not respond to #{k}=.", force_color: :purple
    # TODO - remove now, or when fully deprecated - there will be no verbose
    # check when logger is removed
    mp "BACKTRACE", caller(0).join("\n") if PM.logger.level == :verbose
  end
  element
end
set_attributes(element, args = {}) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 3
def set_attributes(element, args = {})
  args = get_attributes_from_symbol(args)
  ignore_keys = [:transition_style, :presentation_style]
  args.each do |k, v|
    set_attribute(element, k, v) unless ignore_keys.include?(k)
  end
  element.send(:on_styled) if element.respond_to?(:on_styled)
  element
end
view_or_self() click to toggle source
# File lib/ProMotion/styling/styling.rb, line 85
def view_or_self
  self.respond_to?(:view) ? self.view : self
end

Protected Instance Methods

get_attributes_from_symbol(attrs) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 121
def get_attributes_from_symbol(attrs)
  return attrs if attrs.is_a?(Hash)
  mp("#{attrs} styling method is not defined", force_color: :red) unless self.respond_to?(attrs)
  new_attrs = send(attrs)
  mp("#{attrs} should return a hash", force_color: :red) unless new_attrs.is_a?(Hash)
  new_attrs
end
map_resize_symbol(symbol) click to toggle source
# File lib/ProMotion/styling/styling.rb, line 129
def map_resize_symbol(symbol)
  @_resize_symbols ||= {
    left:     UIViewAutoresizingFlexibleLeftMargin,
    right:    UIViewAutoresizingFlexibleRightMargin,
    top:      UIViewAutoresizingFlexibleTopMargin,
    bottom:   UIViewAutoresizingFlexibleBottomMargin,
    width:    UIViewAutoresizingFlexibleWidth,
    height:   UIViewAutoresizingFlexibleHeight
  }
  @_resize_symbols[symbol] || symbol
end