Class: MotionPrime::ViewStyler
Constant Summary
collapse
- ORDER =
%w[font placeholder_font text]
Instance Attribute Summary collapse
Instance Method Summary
collapse
#attributed_string, #extract_attributed_string_options, #html_string
#camelize_factory, #class_factory, #low_camelize_factory
Methods included from HasStyles
#prepare_gradient
#calculate_frame_for
Constructor Details
#initialize(view, parent_bounds = CGRectZero, options = {}) ⇒ ViewStyler
Returns a new instance of ViewStyler.
12
13
14
15
16
17
|
# File 'motion-prime/views/view_styler.rb', line 12
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
#options ⇒ Object
Returns the value of attribute options.
10
11
12
|
# File 'motion-prime/views/view_styler.rb', line 10
def options
@options
end
|
#view ⇒ Object
Returns the value of attribute view.
10
11
12
|
# File 'motion-prime/views/view_styler.rb', line 10
def view
@view
end
|
Instance Method Details
#apply ⇒ Object
19
20
21
22
23
24
|
# File 'motion-prime/views/view_styler.rb', line 19
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
26
27
28
29
30
31
32
|
# File 'motion-prime/views/view_styler.rb', line 26
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
|
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'motion-prime/views/view_styler.rb', line 83
def (options)
text_attributes = [
:text, :html, :line_spacing, :line_height, :underline, :fragment_color,
:text_alignment, :font, :line_break_mode, :number_of_lines
]
attributed_text_options = options.slice(*text_attributes)
if view.is_a?(UIButton)
attributed_text_options[:text] ||= options[:title]
end
options.except!(*text_attributes)
attributed_text_options
end
|
72
73
74
75
76
77
78
79
80
81
|
# File 'motion-prime/views/view_styler.rb', line 72
def (options, prefix = nil)
key = [prefix, 'font'].compact.join('_').to_sym
name_key = [prefix, 'font_name'].compact.join('_').to_sym
size_key = [prefix, 'font_size'].compact.join('_').to_sym
if options.slice(size_key, name_key).any?
font_name = options.delete(name_key) || :system
font_size = options.delete(size_key) || 14
options[key] ||= font_name.uifont(font_size)
end
end
|
#prepare_frame_for(parent_bounds) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'motion-prime/views/view_styler.rb', line 34
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[:left].nil? && !options[:right].nil?
mask |= UIViewAutoresizingFlexibleHeight if options[:height_to_fit].nil? && (!options[:top].nil? && !options[:bottom].nil?)
options[:autoresizingMask] = mask
end
end
|
#prepare_options! ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'motion-prime/views/view_styler.rb', line 48
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 = (options)
html = text_options.delete(:html)
text_options[:text] = html if html
options[:attributed_text] = html ? html_string(text_options) : attributed_string(text_options)
if 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
(options)
(options, 'placeholder')
@options = Hash[options.sort_by { |k,v| ORDER.index(k.to_s).to_i }]
end
|
#set_option(key, value) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'motion-prime/views/view_styler.rb', line 96
def set_option(key, value)
return if ignore_option?(key)
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
|