Class: MotionPrime::ViewStyler

Inherits:
Object
  • Object
show all
Includes:
ElementTextMixin, FrameCalculatorMixin, HasClassFactory, HasStyleOptions, HasStyles
Defined in:
motion-prime/views/view_styler.rb

Constant Summary collapse

ORDER =
%w[
frame
font text title_label title
minimum_value maximum_value value
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasStyleOptions

#extract_font_from

Methods included from ElementTextMixin

#attributed_string, #extract_attributed_string_options, #html_string

Methods included from HasClassFactory

#camelize_factory, #class_factory, #low_camelize_factory, #underscore_factory

Methods included from HasStyles

#prepare_gradient

Methods included from FrameCalculatorMixin

#calculate_frame_for

Constructor Details

#initialize(view, parent_bounds = CGRectZero, options = {}) ⇒ ViewStyler

Returns a new instance of ViewStyler.



17
18
19
20
21
22
# File 'motion-prime/views/view_styler.rb', line 17

def initialize(view, parent_bounds = CGRectZero, options = {})
  @options = Styles.extend_and_normalize_options(options)
  @view = view
  prepare_frame_for(parent_bounds) if @options.delete(:calculate_frame)
  prepare_options!
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'motion-prime/views/view_styler.rb', line 15

def options
  @options
end

#viewObject (readonly)

Returns the value of attribute view.



15
16
17
# File 'motion-prime/views/view_styler.rb', line 15

def view
  @view
end

Instance Method Details

#applyObject



24
25
26
27
28
29
# File 'motion-prime/views/view_styler.rb', line 24

def apply
  converted_options = convert_primitives_to_objects(options)
  converted_options.each do |key, value|
    set_option(key.to_s, value)
  end
end

#convert_primitives_to_objects(options) ⇒ Object



31
32
33
34
35
36
37
# File 'motion-prime/views/view_styler.rb', line 31

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)
    result[k] = v
    result
  end
end

#extract_attributed_text_options(options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'motion-prime/views/view_styler.rb', line 85

def extract_attributed_text_options(options)
  text_attributes = [
    :text, :html, :line_spacing, :line_height, :underline, :fragment_color,
    :text_alignment, :font, :font_name, :font_size, :line_break_mode, :number_of_lines, :text_color
  ]
  attributed_text_options = options.slice(*text_attributes)
  exclude_attributes = text_attributes
  if view.is_a?(UIButton)
    attributed_text_options[:text_color] ||= options[:title_color]
    attributed_text_options[:text] ||= options[:title]
    exclude_attributes.delete(:line_break_mode)
  end
  options.except!(*exclude_attributes)
  attributed_text_options
end

#extract_font_options(options, prefix = nil) ⇒ Object



80
81
82
83
# File 'motion-prime/views/view_styler.rb', line 80

def extract_font_options(options, prefix = nil)
  key = [prefix, 'font'].compact.join('_').to_sym
  options[key] = extract_font_from(options, prefix)
end

#prepare_frame_for(parent_bounds) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'motion-prime/views/view_styler.rb', line 39

def prepare_frame_for(parent_bounds)
  options[:frame] = calculate_frame_for(parent_bounds, options)
  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?
    mask |= UIViewAutoresizingFlexibleRightMargin if options[:right].nil?
    mask |= UIViewAutoresizingFlexibleWidth if options[:width].nil? && (!options[:left].nil? && !options[:right].nil?)
    mask |= UIViewAutoresizingFlexibleHeight if options[:height].nil? && options[:height_to_fit].nil? && (!options[:top].nil? && !options[:bottom].nil?)
    options[:autoresizingMask] = mask
  end
end

#prepare_options!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'motion-prime/views/view_styler.rb', line 53

def prepare_options!
  if options[:size_to_fit]
    options[:line_break_mode] ||= :word_wrap
    options[:number_of_lines] ||= 0 if view.is_a?(UILabel)
  end

  if options.slice(:html, :line_spacing, :line_height, :underline, :fragment_color).any?
    text_options = extract_attributed_text_options(options)
    html = text_options.delete(:html)
    text_options[:text] = html if html
    options[:attributed_text] = html ? html_string(text_options) : attributed_string(text_options)

    # ios 7 bug fix when text is invisible
    if view.is_a?(UILabel) && text_options.slice(:line_height, :line_spacing, :text_alignment, :line_break_mode).any? && options.fetch(:number_of_lines, 1) == 1
      options[:number_of_lines] = 0
    end
  end
  # Fix issue overriding background color
  if options[:background_image].present?
    options.delete(:background_color)
  end
  extract_font_options(options)
  extract_font_options(options, 'placeholder')

  @options = Hash[options.sort_by {|k,v| ORDER.index(k.to_s) || ORDER.count }]
end

#set_option(key, value) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'motion-prime/views/view_styler.rb', line 101

def set_option(key, value)
  # return if value.nil?
  # ignore options
  return if ignore_option?(key) || value.nil?

  # apply options
  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