Module: ProMotion::Styling

Included in:
ScreenModule, Table, TableViewCellModule
Defined in:
lib/ProMotion/styling/styling.rb

Instance Method Summary collapse

Instance Method Details

#add(element, attrs = {}) ⇒ Object



59
60
61
# File 'lib/ProMotion/styling/styling.rb', line 59

def add(element, attrs = {})
  add_to view_or_self, element, attrs
end

#add_to(parent_element, elements, attrs = {}) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/ProMotion/styling/styling.rb', line 67

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
  end
  elements
end

#camelize(str) ⇒ Object

Turns a snake_case string into a camelCase string.



106
107
108
# File 'lib/ProMotion/styling/styling.rb', line 106

def camelize(str)
  str.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
end

#closest_parent(type, this_view = nil) ⇒ Object

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



50
51
52
53
54
55
56
57
# File 'lib/ProMotion/styling/styling.rb', line 50

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) ⇒ Object



40
41
42
# File 'lib/ProMotion/styling/styling.rb', line 40

def content_height(view)
  content_max(view, :height)
end

#content_max(view, mode = :height) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ProMotion/styling/styling.rb', line 28

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) ⇒ Object



44
45
46
# File 'lib/ProMotion/styling/styling.rb', line 44

def content_width(view)
  content_max(view, :width)
end

#hex_color(str) ⇒ Object

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ProMotion/styling/styling.rb', line 90

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) ⇒ Object



63
64
65
# File 'lib/ProMotion/styling/styling.rb', line 63

def remove(elements)
  Array(elements).each(&:removeFromSuperview)
end

#rgb_color(r, g, b) ⇒ Object

These three color methods are stolen from BubbleWrap.



81
82
83
# File 'lib/ProMotion/styling/styling.rb', line 81

def rgb_color(r,g,b)
  rgba_color(r,g,b,1)
end

#rgba_color(r, g, b, a) ⇒ Object



85
86
87
88
# File 'lib/ProMotion/styling/styling.rb', line 85

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) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ProMotion/styling/styling.rb', line 9

def set_attribute(element, k, v)
  return element 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)
  else
    # Doesn't respond. Check if snake case.
    set_attribute(element, camelize(k), v) if k.to_s.include?("_")
  end
  element
end

#set_attributes(element, args = {}) ⇒ Object



3
4
5
6
7
# File 'lib/ProMotion/styling/styling.rb', line 3

def set_attributes(element, args = {})
  args = get_attributes_from_symbol(args)
  args.each { |k, v| set_attribute(element, k, v) }
  element
end

#view_or_selfObject



76
77
78
# File 'lib/ProMotion/styling/styling.rb', line 76

def view_or_self
  self.respond_to?(:view) ? self.view : self
end