motion-mastr 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +127 -0
  3. data/lib/motion-mastr.rb +10 -0
  4. data/lib/motion-mastr/attributes/attachment_attribute.rb +15 -0
  5. data/lib/motion-mastr/attributes/background_color_attribute.rb +16 -0
  6. data/lib/motion-mastr/attributes/baseline_offset_attribute.rb +20 -0
  7. data/lib/motion-mastr/attributes/expansion_attribute.rb +20 -0
  8. data/lib/motion-mastr/attributes/font_attribute.rb +16 -0
  9. data/lib/motion-mastr/attributes/foreground_color_attribute.rb +16 -0
  10. data/lib/motion-mastr/attributes/kern_attribute.rb +19 -0
  11. data/lib/motion-mastr/attributes/ligature_attribute.rb +19 -0
  12. data/lib/motion-mastr/attributes/link_attribute.rb +16 -0
  13. data/lib/motion-mastr/attributes/obliqueness_attribute.rb +20 -0
  14. data/lib/motion-mastr/attributes/paragraph_style_attribute.rb +221 -0
  15. data/lib/motion-mastr/attributes/shadow_attribute.rb +51 -0
  16. data/lib/motion-mastr/attributes/strikethrough_color_attribute.rb +16 -0
  17. data/lib/motion-mastr/attributes/strikethrough_style_attribute.rb +22 -0
  18. data/lib/motion-mastr/attributes/stroke_color_attribute.rb +16 -0
  19. data/lib/motion-mastr/attributes/stroke_width_attribute.rb +20 -0
  20. data/lib/motion-mastr/attributes/text_effect_attribute.rb +15 -0
  21. data/lib/motion-mastr/attributes/underline_color_attribute.rb +16 -0
  22. data/lib/motion-mastr/attributes/underline_style_attribute.rb +22 -0
  23. data/lib/motion-mastr/attributes/underline_style_helper.rb +21 -0
  24. data/lib/motion-mastr/mastr.rb +15 -0
  25. data/lib/motion-mastr/mastr_builder.rb +86 -0
  26. data/lib/motion-mastr/version.rb +3 -0
  27. metadata +83 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2833f7e9ad2ff27188a0dd9a720cb3372f2ed2c3
4
+ data.tar.gz: 26e8487689649201c36cc45b47c415c99ef9732e
5
+ SHA512:
6
+ metadata.gz: f596e78fcf9c6e9dae1f8b18dfbf8cf1bb002cbabe2502f3e508244d9aebdaf1aa16fd50ab0fe663516cd8c85c713a9c7a70f2017d4efb659871f7e32d39be11
7
+ data.tar.gz: b42fab78b38998393917ccc30d623b56288d03783ce8f6a91536efb161a57f8135ce5acf0d3f2c3e6df3d40c40667c67012c39a27c6d19ffffe936f402467146
@@ -0,0 +1,127 @@
1
+ # motion-mastr
2
+ [![Gem Version](https://img.shields.io/gem/v/motion-mastr.svg?style=flat)](https://rubygems.org/gems/motion-mastr)
3
+ [![Build Status](https://img.shields.io/travis/skellock/motion-mastr.svg?style=flat)](https://travis-ci.org/skellock/motion-mastr)
4
+
5
+ A RubyMotion library for iOS 7+ to own NSMutableAttributedString.
6
+
7
+ MASTR = Mutable Attributed STRing
8
+
9
+ <img src="./_pics/mastr.gif" alt="Demo" />
10
+
11
+
12
+ # Installation
13
+
14
+ Add this to your Gemfile:
15
+
16
+ ```ruby
17
+ gem 'motion-mastr'
18
+ ```
19
+
20
+ Then run this to install it:
21
+
22
+ ```
23
+ bundle
24
+ ```
25
+
26
+ # Requirements
27
+
28
+ * iOS 7 or higher
29
+
30
+
31
+ # Usage
32
+
33
+ ```ruby
34
+ include MotionMastr
35
+ ```
36
+
37
+ This is a MastrBuilder.
38
+ ```ruby
39
+ mastr # => MastrBuilder
40
+ ```
41
+
42
+ You'll use this to build your `NSMutableAttributeString`s.
43
+ ```ruby
44
+ mastr.build # => NSMutableAttributeString
45
+ ```
46
+
47
+ Feed it a string.
48
+ ```ruby
49
+ mastr("hello").build
50
+ ```
51
+
52
+ Feed it styles too.
53
+ ```ruby
54
+ mastr("Grimace", foreground_color: UIColor.purpleColor).build
55
+ ```
56
+
57
+ Keep adding pieces until you get what you want.
58
+ ```ruby
59
+ mastr.add("hi").add("there").add("!").build
60
+ ```
61
+
62
+ Styles inside mastr() are default styles.
63
+ ```ruby
64
+ blue = UIColor.blueColor
65
+ mastr("blue", foreground_color: blue).add(" blue ").add(" blue ").build
66
+ ```
67
+
68
+ Override defaults while adding pieces.
69
+ ```ruby
70
+ red = UIColor.redColor
71
+ green = UIColor.greenColor
72
+ mastr("red light", foreground_color:red)
73
+ .add("green light", foreground_color:green)
74
+ .add("red light")
75
+ .add("red light")
76
+ .build
77
+ ```
78
+
79
+ # Supported Styles
80
+
81
+ ```ruby
82
+ alignment # :left, :right, :center
83
+ attachment # NSTextAttachment
84
+ background_color # UIColor
85
+ base_writing_direction # :natural, :left_to_right, :right_to_left
86
+ baseline_offset # :default or Float
87
+ default_tab_interval # :default or 0.0 and up
88
+ expansion # :default or Float
89
+ first_line_head_indent # 0.0 and up
90
+ font # UIFont
91
+ foreground_color # UIColor
92
+ head_indent # 0.0 and up
93
+ hyphenation_factor # 0.0 up to 1.0
94
+ kern # :default, Float
95
+ ligature # :off, :on
96
+ line_break_mode # :word, :char, :clip, :head, :tail, :middle
97
+ line_height_multiple # :default, 0.0 and up
98
+ line_spacing # 0.0 and up
99
+ link # NSURL or NSString
100
+ maximum_line_height # :off, 0.0 and up
101
+ minimum_line_height # :off, 0.0 and up
102
+ obliqueness # :default, Float
103
+ paragraph_spacing # :default, 0.0 and up
104
+ paragraph_spacing_before # :default, 0.0 and up
105
+ shadow_blur_radius # 0.0 and up
106
+ shadow_color # UIColor
107
+ shadow_offset # CGSize, [Float, Float], Float
108
+ strikethrough_color # UIColor
109
+ strikethrough_style # :style_none, :style_single, :style_thick, :style_double or an array including those + [:pattern_solid :pattern_dot :pattern_dash :pattern_dash_dot :pattern_dash_dot_dot :by_word]
110
+ stroke_color # UIColor
111
+ stroke_width # :default, Float
112
+ tail_indent # 0.0 and up
113
+ text_effect # :letterpress
114
+ underline_color # UIColor
115
+ underline_style # :style_none, :style_single, :style_thick, :style_double or an array including those + [:pattern_solid :pattern_dot :pattern_dash :pattern_dash_dot :pattern_dash_dot_dot :by_word]
116
+ ```
117
+
118
+ For details, check out the NSAttributedString docs. Or just play around.
119
+
120
+ # Wierd Things
121
+
122
+ * Setting text_effect makes the CPU work hard, not sure why yet.
123
+ * Shadow properties aren't working when assigning to a UILabel, not sure why yet.
124
+ * Go easy on obliqueness, don't get hurt.
125
+
126
+
127
+
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ unless defined?(Motion::Project::Config)
4
+ raise "This file must be required within a RubyMotion project Rakefile."
5
+ end
6
+
7
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
8
+ Motion::Project::App.setup do |app|
9
+ app.files.unshift(Dir.glob(File.join(lib_dir_path, "motion-mastr/**/*.rb")))
10
+ end
@@ -0,0 +1,15 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSAttachmentAttributeName
5
+ #
6
+ # :attachment = NSTextAttachment
7
+ #
8
+ #
9
+ def attachment_attribute(styles={})
10
+ return nil if styles.nil?
11
+ styles[:attachment].is_a?(NSTextAttachment) ? styles[:attachment] : nil
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSBackgroundColorAttributeName
5
+ #
6
+ # :background_color = UIColor
7
+ #
8
+ def background_color_attribute(styles={})
9
+ return nil if styles.nil?
10
+ value = styles[:background_color]
11
+ return nil unless value && value.is_a?(UIColor)
12
+ value
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSBaselineOffsetAttributeName
5
+ #
6
+ # :baseline_offset
7
+ # * 0, :default
8
+ # * A Float
9
+ #
10
+ def baseline_offset_attribute(styles={})
11
+ return nil if styles.nil?
12
+ return nil if styles[:baseline_offset].nil?
13
+ value = styles[:baseline_offset]
14
+ return 0 if [0, :default].include?(value)
15
+ return nil unless value.respond_to?(:to_f)
16
+ return value.to_f
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSExpansionAttributeName
5
+ #
6
+ # :expansion
7
+ # * 0, :default
8
+ # * A Float
9
+ #
10
+ def expansion_attribute(styles={})
11
+ return nil if styles.nil?
12
+ return nil if styles[:expansion].nil?
13
+ value = styles[:expansion]
14
+ return 0 if [0, :default].include?(value)
15
+ return nil unless value.respond_to?(:to_f)
16
+ return value.to_f
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSFontAttributeName
5
+ #
6
+ # :font = UIFont
7
+ #
8
+ def font_attribute(styles={})
9
+ return nil if styles.nil?
10
+ value = styles[:font]
11
+ return nil unless value && value.is_a?(UIFont)
12
+ value
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSForegroundColorAttributeName
5
+ #
6
+ # :foreground_color = UIColor
7
+ #
8
+ def foreground_color_attribute(styles={})
9
+ return nil if styles.nil?
10
+ value = styles[:foreground_color]
11
+ return nil unless value && value.is_a?(UIColor)
12
+ value
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSKernAttributeName
5
+ #
6
+ # :kern or :kerning
7
+ # * 0, :off, :none, :default
8
+ # * A Float
9
+ #
10
+ def kern_attribute(styles={})
11
+ return nil if styles.nil?
12
+ return unless styles[:kern] || styles[:kerning]
13
+ value = styles[:kern] || styles[:kerning]
14
+ return 0 if [0, :off, :none, :default].include?(value)
15
+ return value.to_f
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSLigatureAttributeName
5
+ #
6
+ # :ligature
7
+ # = 0, :off, :none
8
+ # = 1, :on, :default
9
+ #
10
+ def ligature_attribute(styles={})
11
+ return nil if styles.nil?
12
+ value = styles[:ligature]
13
+ return 0 if [0, :off, :none].include?(value)
14
+ return 1 if [1, :on, :default].include?(value)
15
+ nil
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSLinkAttributeName
5
+ #
6
+ # :link = NSString or NSURL
7
+ #
8
+ #
9
+ def link_attribute(styles={})
10
+ return nil if styles.nil?
11
+ return styles[:link] if styles[:link].is_a?(NSURL) || styles[:link].is_a?(String)
12
+ nil
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSObliquenessAttributeName
5
+ #
6
+ # :obliqueness
7
+ # * 0, :default
8
+ # * A Float
9
+ #
10
+ def obliqueness_attribute(styles={})
11
+ return nil if styles.nil?
12
+ return nil if styles[:obliqueness].nil?
13
+ value = styles[:obliqueness]
14
+ return 0 if [0, :default].include?(value)
15
+ return nil unless value.respond_to?(:to_f)
16
+ return value.to_f
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,221 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSParagraphStyleAttributeName
5
+ #
6
+ # :alignment
7
+ # :left, :right, :center, :centered
8
+ #
9
+ # :first_line_head_indent
10
+ # non-negative Float
11
+ #
12
+ # :head_indent
13
+ # non-negative Float
14
+ #
15
+ # :tail_indent
16
+ # non-negative Float
17
+ #
18
+ # :line_break_mode
19
+ #
20
+ # :by_word, :by_word_wrap, :by_word_wrapping, :word, :word_wrapping, :word_wrap
21
+ # :by_char, :by_char_wrapping, :char
22
+ # :clip, :clipping, :by_clipping
23
+ # :by_truncating_head, :head
24
+ # :by_truncating_tail, :tail
25
+ # :by_truncating_middle, :middle
26
+ #
27
+ # :maximum_line_height
28
+ #
29
+ # 0, :off, :infinite, :none
30
+ # or a Float
31
+ #
32
+ # :minimum_line_height
33
+ #
34
+ # 0, :off, :infinite, :none
35
+ # or a Float
36
+ #
37
+ # :line_spacing
38
+ #
39
+ # 0 or a positive Float
40
+ #
41
+ # :paragraph_spacing
42
+ #
43
+ # 0 or a positive Float
44
+ #
45
+ # :paragraph_spacing_before
46
+ #
47
+ # 0, :default
48
+ # or a positive Float
49
+ #
50
+ # :base_writing_direction
51
+ #
52
+ # :natural, :default
53
+ # :left, :left_to_right, :l2r
54
+ # :right, :right_to_left, :r2l
55
+ #
56
+ # :line_height_multiple
57
+ #
58
+ # 0, :default
59
+ # or a positive Float
60
+ #
61
+ # :default_tab_interval
62
+ #
63
+ # 0, :default
64
+ # or a positive Float
65
+ #
66
+ # :hyphenation_factor
67
+ # 0, :deffault
68
+ # or a positive Float up to 1.0
69
+ #
70
+ def paragraph_style_attribute(styles={})
71
+ return nil if styles.nil?
72
+
73
+ # search for valid paragraph-related keys
74
+ style_keys = styles.keys
75
+ paragraph_related_keys = [
76
+ :alignment,
77
+ :first_line_head_indent,
78
+ :head_indent,
79
+ :tail_indent,
80
+ :line_break_mode,
81
+ :maximum_line_height,
82
+ :minimum_line_height,
83
+ :line_spacing,
84
+ :paragraph_spacing,
85
+ :paragraph_spacing_before,
86
+ :base_writing_direction,
87
+ :line_height_multiple,
88
+ :default_tab_interval,
89
+ :hyphenation_factor,
90
+ ]
91
+ contains_paragraph_styling = (style_keys - paragraph_related_keys).length != style_keys.length
92
+ return nil unless contains_paragraph_styling
93
+
94
+ # start with logical defaults
95
+ paragraph = NSParagraphStyle.defaultParagraphStyle.mutableCopy
96
+
97
+ # alignment
98
+ if styles[:alignment]
99
+ case styles[:alignment]
100
+ when :right then paragraph.alignment = NSTextAlignmentRight
101
+ when :center, :centered, :centre, :centred then paragraph.alignment = NSTextAlignmentCenter
102
+ when :left then paragraph.alignment = NSTextAlignmentLeft
103
+ end
104
+ end
105
+
106
+ # first line head indent
107
+ if styles[:first_line_head_indent]
108
+ indent = styles[:first_line_head_indent].to_f
109
+ indent = 0 if indent < 0
110
+ paragraph.firstLineHeadIndent = indent
111
+ end
112
+
113
+ # head indent
114
+ if styles[:head_indent]
115
+ indent = styles[:head_indent].to_f
116
+ indent = 0 if indent < 0
117
+ paragraph.headIndent = indent
118
+ end
119
+
120
+ # tail indent
121
+ if styles[:tail_indent]
122
+ indent = styles[:tail_indent].to_f
123
+ indent = 0 if indent < 0
124
+ paragraph.tailIndent = indent
125
+ end
126
+
127
+ # line break mode
128
+ if styles[:line_break_mode]
129
+ case styles[:line_break_mode]
130
+ when :by_word, :by_word_wrap, :by_word_wrapping, :word, :word_wrapping, :word_wrap then paragraph.lineBreakMode = NSLineBreakByWordWrapping
131
+ when :by_char, :by_char_wrapping, :char then paragraph.lineBreakMode = NSLineBreakByCharWrapping
132
+ when :clip, :clipping, :by_clipping then paragraph.lineBreakMode = NSLineBreakByClipping
133
+ when :by_truncating_head, :head then paragraph.lineBreakMode = NSLineBreakByTruncatingHead
134
+ when :by_truncating_tail, :tail then paragraph.lineBreakMode = NSLineBreakByTruncatingTail
135
+ when :by_truncating_middle, :middle then paragraph.lineBreakMode = NSLineBreakByTruncatingMiddle
136
+ end
137
+ end
138
+
139
+ # maximum line height
140
+ if styles[:maximum_line_height]
141
+ maximum_line_height = styles[:maximum_line_height]
142
+ maximum_line_height = 0 if [:off, :infinite, 0, :none].include?(maximum_line_height)
143
+ maximum_line_height ||= maximum_line_height.to_f
144
+ maximum_line_height = 0 if maximum_line_height < 0
145
+ paragraph.maximumLineHeight = maximum_line_height
146
+ end
147
+
148
+ # minimum line height
149
+ if styles[:minimum_line_height]
150
+ minimum_line_height = styles[:minimum_line_height]
151
+ minimum_line_height = 0 if [:off, :infinite, 0, :none].include?(minimum_line_height)
152
+ minimum_line_height ||= minimum_line_height.to_f
153
+ minimum_line_height = 0 if minimum_line_height < 0
154
+ paragraph.minimumLineHeight = minimum_line_height
155
+ end
156
+
157
+ # line spacing
158
+ if styles[:line_spacing]
159
+ line_spacing = styles[:line_spacing].to_f
160
+ line_spacing = 0 if line_spacing < 0
161
+ paragraph.lineSpacing = line_spacing
162
+ end
163
+
164
+ # paragraph spacing
165
+ if styles[:paragraph_spacing]
166
+ paragraph_spacing = styles[:paragraph_spacing].to_f
167
+ paragraph_spacing = 0 if paragraph_spacing < 0
168
+ paragraph.paragraphSpacing = paragraph_spacing
169
+ end
170
+
171
+ # paragraph spacing before
172
+ if styles[:paragraph_spacing_before]
173
+ paragraph_spacing_before = styles[:paragraph_spacing_before]
174
+ paragraph_spacing_before = 0 if [0, :default].include?(paragraph_spacing_before)
175
+ paragraph_spacing_before ||= styles[:paragraph_spacing_before].to_f
176
+ paragraph_spacing_before = 0 if paragraph_spacing_before < 0
177
+ paragraph.paragraphSpacingBefore = paragraph_spacing_before
178
+ end
179
+
180
+ # base writing direction
181
+ if styles[:base_writing_direction]
182
+ case styles[:base_writing_direction]
183
+ when :natural, :default then paragraph.baseWritingDirection = NSWritingDirectionNatural
184
+ when :left, :left_to_right, :l2r then paragraph.baseWritingDirection = NSWritingDirectionLeftToRight
185
+ when :right, :right_to_left, :r2l then paragraph.baseWritingDirection = NSWritingDirectionRightToLeft
186
+ end
187
+ end
188
+
189
+ # line height multiple
190
+ if styles[:line_height_multiple]
191
+ line_height_multiple = styles[:line_height_multiple]
192
+ line_height_multiple = 0 if [0, :default].include?(line_height_multiple)
193
+ line_height_multiple ||= styles[:line_height_multiple].to_f
194
+ line_height_multiple = 0 if line_height_multiple < 0
195
+ paragraph.lineHeightMultiple = line_height_multiple
196
+ end
197
+
198
+ # default tab interval
199
+ if styles[:default_tab_interval]
200
+ default_tab_interval = styles[:default_tab_interval]
201
+ default_tab_interval = 0 if [0, :default].include?(default_tab_interval)
202
+ default_tab_interval ||= styles[:default_tab_interval].to_f
203
+ default_tab_interval = 0 if default_tab_interval < 0
204
+ paragraph.defaultTabInterval = default_tab_interval
205
+ end
206
+
207
+ # hyphenation factor
208
+ if styles[:hyphenation_factor]
209
+ hyphenation_factor = styles[:hyphenation_factor]
210
+ hyphenation_factor = 0 if [0, :default].include?(hyphenation_factor)
211
+ hyphenation_factor ||= styles[:hyphenation_factor].to_f
212
+ hyphenation_factor = 0 if hyphenation_factor < 0
213
+ hyphenation_factor = 1.0 if hyphenation_factor > 1
214
+ paragraph.hyphenationFactor = hyphenation_factor
215
+ end
216
+
217
+ paragraph
218
+ end
219
+
220
+ end
221
+ end
@@ -0,0 +1,51 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSShadowAttributeName
5
+ #
6
+ # :shadow_offset
7
+ # CGSize
8
+ #
9
+ def shadow_attribute(styles={})
10
+ return nil if styles.nil?
11
+
12
+ # search for valid shadow-related keys
13
+ style_keys = styles.keys
14
+ shadow_keys = [:shadow_offset, :shadow_blur_radius, :shadow_color]
15
+ contains_shadow_styling = (style_keys - shadow_keys).length != style_keys.length
16
+ return nil unless contains_shadow_styling
17
+
18
+ # create our shadow
19
+ shadow = NSShadow.alloc.init
20
+
21
+ # offset
22
+ if styles[:shadow_offset]
23
+ offset = styles[:shadow_offset]
24
+ if offset.is_a? CGSize
25
+ shadow.shadowOffset = offset
26
+ elsif offset.is_a? Array
27
+ shadow.shadowOffset = offset if offset.length == 2
28
+ elsif offset.is_a? Fixnum
29
+ shadow.shadowOffset = [offset, offset]
30
+ elsif offset.is_a? Float
31
+ shadow.shadowOffset = [offset, offset]
32
+ end
33
+ end
34
+
35
+ # blur
36
+ if styles[:shadow_blur_radius]
37
+ blur = styles[:shadow_blur_radius]
38
+ blur = 0 if [0, :default, :none, :off].include?(blur)
39
+ blur ||= blur.to_f
40
+ blur = 0 if blur < 0
41
+ shadow.shadowBlurRadius = blur
42
+ end
43
+
44
+ # color
45
+ shadow.shadowColor = styles[:shadow_color] if styles[:shadow_color].is_a? UIColor
46
+
47
+ shadow
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,16 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSStrikethroughColorAttributeName
5
+ #
6
+ # :strikethrough_color = UIColor
7
+ #
8
+ def strikethrough_color_attribute(styles={})
9
+ return nil if styles.nil?
10
+ value = styles[:strikethrough_color]
11
+ return nil unless value && value.is_a?(UIColor)
12
+ value
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSStrikethroughStyleAttribute
5
+ #
6
+ # :strikethrough_style is either a Symbol or Array of symbols which will be OR'd together
7
+ #
8
+ def strikethrough_style_attribute(styles={})
9
+ return nil if styles.nil?
10
+ value = styles[:strikethrough_style]
11
+ return underline_style_value(value) if value.is_a? Symbol
12
+ if value.is_a? Array
13
+ bitsauce = 0
14
+ value.each { |s| bitsauce |= underline_style_value(s) }
15
+ bitsauce
16
+ else
17
+ nil
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSStrokeColorAttributeName
5
+ #
6
+ # :stroke_color = UIColor
7
+ #
8
+ def stroke_color_attribute(styles={})
9
+ return nil if styles.nil?
10
+ value = styles[:stroke_color]
11
+ return nil unless value && value.is_a?(UIColor)
12
+ value
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSStrokeWidthAttributeName
5
+ #
6
+ # :stroke_width
7
+ # * 0, :default
8
+ # * A Float
9
+ #
10
+ def stroke_width_attribute(styles={})
11
+ return nil if styles.nil?
12
+ return nil if styles[:stroke_width].nil?
13
+ value = styles[:stroke_width]
14
+ return 0 if [0, :default].include?(value)
15
+ return nil unless value.respond_to?(:to_f)
16
+ return value.to_f
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSTextEffectAttributeName
5
+ #
6
+ # :text_effect = :symbol
7
+ #
8
+ def text_effect_attribute(styles={})
9
+ return nil if styles.nil?
10
+ return NSTextEffectLetterpressStyle if [:letterpress, :letterpress_style].include?(styles[:text_effect])
11
+ nil
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSUnderlineColorAttributeName
5
+ #
6
+ # :underline_color = UIColor
7
+ #
8
+ def underline_color_attribute(styles={})
9
+ return nil if styles.nil?
10
+ value = styles[:underline_color]
11
+ return nil unless value && value.is_a?(UIColor)
12
+ value
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ # NSUnderlineStyleAttribute
5
+ #
6
+ # :underline_style is either a Symbol or Array of symbols which will be OR'd together
7
+ #
8
+ def underline_style_attribute(styles={})
9
+ return nil if styles.nil?
10
+ value = styles[:underline_style]
11
+ return underline_style_value(value) if value.is_a? Symbol
12
+ if value.is_a? Array
13
+ bitsauce = 0
14
+ value.each { |s| bitsauce |= underline_style_value(s) }
15
+ bitsauce
16
+ else
17
+ nil
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module MotionMastr
2
+ module Attributes
3
+
4
+ def underline_style_value(value)
5
+ case value
6
+ when :style_none, :none then NSUnderlineStyleNone
7
+ when :style_single, :single then NSUnderlineStyleSingle
8
+ when :style_thick, :thick then NSUnderlineStyleThick
9
+ when :style_double, :double then NSUnderlineStyleDouble
10
+ when :pattern_solid, :solid then NSUnderlinePatternSolid
11
+ when :pattern_dot, :dot then NSUnderlinePatternDot
12
+ when :pattern_dash, :dash then NSUnderlinePatternDash
13
+ when :pattern_dash_dot, :dash_dot then NSUnderlinePatternDashDot
14
+ when :pattern_dash_dot_dot, :dash_dot_dot then NSUnderlinePatternDashDotDot
15
+ when :by_word then NSUnderlineByWord
16
+ else NSUnderlineStyleNone
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module MotionMastr
2
+
3
+ # make a MastrBuilder
4
+ def mastr(text=nil, default_styles={})
5
+ b = MastrBuilder.new default_styles
6
+ b.add text
7
+ b
8
+ end
9
+
10
+ # make and build a NSMutableAttributedString
11
+ def mastr!(text=nil, default_styles={})
12
+ mastr(text, default_styles).build
13
+ end
14
+
15
+ end
@@ -0,0 +1,86 @@
1
+ module MotionMastr
2
+ class MastrBuilder
3
+
4
+ include Attributes
5
+
6
+ attr_reader :default_styles, :pieces
7
+
8
+ ATTRIBUTES = {
9
+ font_attribute: NSFontAttributeName,
10
+ foreground_color_attribute: NSForegroundColorAttributeName,
11
+ background_color_attribute: NSBackgroundColorAttributeName,
12
+ stroke_color_attribute: NSStrokeColorAttributeName,
13
+ strikethrough_color_attribute: NSStrikethroughColorAttributeName,
14
+ underline_color_attribute: NSUnderlineColorAttributeName,
15
+ ligature_attribute: NSLigatureAttributeName,
16
+ kern_attribute: NSKernAttributeName,
17
+ paragraph_style_attribute: NSParagraphStyleAttributeName,
18
+ strikethrough_style_attribute: NSStrikethroughStyleAttributeName,
19
+ underline_style_attribute: NSUnderlineStyleAttributeName,
20
+ stroke_width_attribute: NSStrokeWidthAttributeName,
21
+ shadow_attribute: NSShadowAttributeName,
22
+ text_effect_attribute: NSTextEffectAttributeName,
23
+ attachment_attribute: NSAttachmentAttributeName,
24
+ link_attribute: NSLinkAttributeName,
25
+ baseline_offset_attribute: NSBaselineOffsetAttributeName,
26
+ obliqueness_attribute: NSObliquenessAttributeName,
27
+ expansion_attribute: NSExpansionAttributeName,
28
+ }
29
+
30
+ def initialize(default_styles={})
31
+ @default_styles = default_styles || {}
32
+ @pieces = []
33
+ end
34
+
35
+ # write to default styles
36
+ def []=(key, value)
37
+ @default_styles[key] = value
38
+ end
39
+
40
+ # read from default styles
41
+ def [](key)
42
+ @default_styles[key]
43
+ end
44
+
45
+ # add a piece of text and optional styles
46
+ def add(text, styles={})
47
+ @pieces << {text: text, styles: styles} unless text.nil?
48
+ self
49
+ end
50
+
51
+ def build
52
+ # weld up the text pieces
53
+ text = @pieces.map { |p| p[:text] }.join
54
+
55
+ # compose a attribute string
56
+ result = NSMutableAttributedString.alloc.initWithString(text)
57
+
58
+ # style it with the default styles
59
+ apply_attributes result, 0, text.length, @default_styles
60
+
61
+ # apply the styles from the pieces
62
+ idx = 0
63
+ @pieces.each do |piece|
64
+ apply_attributes result, idx, piece[:text].length, piece[:styles]
65
+ idx += piece[:text].length
66
+ end
67
+
68
+ result
69
+ end
70
+
71
+ # applies styles in a range to the attributed string
72
+ def apply_attributes(attributed_string, start, length, styles)
73
+ return unless attributed_string && start && length && styles # sanity
74
+ return unless start >= 0 && length > 0 && styles.length > 0 # minimums
75
+ return unless start + length <= attributed_string.length # maximums
76
+
77
+ range = [start, length]
78
+
79
+ ATTRIBUTES.each_pair do |method_name, attribute_name|
80
+ value = send method_name, styles
81
+ attributed_string.addAttribute(attribute_name, value: value, range: range) if value
82
+ end
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module MotionMastr
2
+ VERSION = "0.2.0" unless defined?(MotionMastr::VERSION)
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-mastr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Kellock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ description: Get your NSMutableAttributedString on!
28
+ email:
29
+ - steve@kellock.ca
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - lib/motion-mastr.rb
36
+ - lib/motion-mastr/attributes/attachment_attribute.rb
37
+ - lib/motion-mastr/attributes/background_color_attribute.rb
38
+ - lib/motion-mastr/attributes/baseline_offset_attribute.rb
39
+ - lib/motion-mastr/attributes/expansion_attribute.rb
40
+ - lib/motion-mastr/attributes/font_attribute.rb
41
+ - lib/motion-mastr/attributes/foreground_color_attribute.rb
42
+ - lib/motion-mastr/attributes/kern_attribute.rb
43
+ - lib/motion-mastr/attributes/ligature_attribute.rb
44
+ - lib/motion-mastr/attributes/link_attribute.rb
45
+ - lib/motion-mastr/attributes/obliqueness_attribute.rb
46
+ - lib/motion-mastr/attributes/paragraph_style_attribute.rb
47
+ - lib/motion-mastr/attributes/shadow_attribute.rb
48
+ - lib/motion-mastr/attributes/strikethrough_color_attribute.rb
49
+ - lib/motion-mastr/attributes/strikethrough_style_attribute.rb
50
+ - lib/motion-mastr/attributes/stroke_color_attribute.rb
51
+ - lib/motion-mastr/attributes/stroke_width_attribute.rb
52
+ - lib/motion-mastr/attributes/text_effect_attribute.rb
53
+ - lib/motion-mastr/attributes/underline_color_attribute.rb
54
+ - lib/motion-mastr/attributes/underline_style_attribute.rb
55
+ - lib/motion-mastr/attributes/underline_style_helper.rb
56
+ - lib/motion-mastr/mastr.rb
57
+ - lib/motion-mastr/mastr_builder.rb
58
+ - lib/motion-mastr/version.rb
59
+ homepage: https://github.com/skellock/motion-mastr
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.4.5
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: A RubyMotion library for iOS 7+ to own NSMutableAttributedString.
83
+ test_files: []