class MotionMastr::MastrBuilder
Constants
- ATTRIBUTES
Attributes
default_styles[R]
pieces[R]
Public Class Methods
new(default_styles={})
click to toggle source
# File lib/motion-mastr/mastr_builder.rb, line 30 def initialize(default_styles={}) @default_styles = default_styles || {} @pieces = [] end
Public Instance Methods
[](key)
click to toggle source
read from default styles
# File lib/motion-mastr/mastr_builder.rb, line 41 def [](key) @default_styles[key] end
[]=(key, value)
click to toggle source
write to default styles
# File lib/motion-mastr/mastr_builder.rb, line 36 def []=(key, value) @default_styles[key] = value end
add(text, styles={})
click to toggle source
add a piece of text and optional styles
# File lib/motion-mastr/mastr_builder.rb, line 46 def add(text, styles={}) @pieces << {text: text, styles: styles} unless text.nil? self end
apply_attributes(attributed_string, start, length, styles)
click to toggle source
applies styles in a range to the attributed string
# File lib/motion-mastr/mastr_builder.rb, line 72 def apply_attributes(attributed_string, start, length, styles) return unless attributed_string && start && length && styles # sanity return unless start >= 0 && length > 0 && styles.length > 0 # minimums return unless start + length <= attributed_string.length # maximums range = [start, length] ATTRIBUTES.each_pair do |method_name, attribute_name| value = send method_name, styles attributed_string.addAttribute(attribute_name, value: value, range: range) if value end end
build()
click to toggle source
# File lib/motion-mastr/mastr_builder.rb, line 51 def build # weld up the text pieces text = @pieces.map { |p| p[:text] }.join # compose a attribute string result = NSMutableAttributedString.alloc.initWithString(text) # style it with the default styles apply_attributes result, 0, text.length, @default_styles # apply the styles from the pieces idx = 0 @pieces.each do |piece| apply_attributes result, idx, piece[:text].length, piece[:styles] idx += piece[:text].length end result end