module MotionMastr::Attributes

Public Instance Methods

attachment_attribute(styles={}) click to toggle source

NSAttachmentAttributeName

:attachment = NSTextAttachment

# File lib/motion-mastr/attributes/attachment_attribute.rb, line 9
def attachment_attribute(styles={})
  return nil if styles.nil?
  styles[:attachment].is_a?(NSTextAttachment) ? styles[:attachment] : nil
end
background_color_attribute(styles={}) click to toggle source

NSBackgroundColorAttributeName

:background_color = UIColor

# File lib/motion-mastr/attributes/background_color_attribute.rb, line 8
def background_color_attribute(styles={})
  return nil if styles.nil?
  value = styles[:background_color]
  return nil unless value && value.is_a?(UIColor)
  value
end
baseline_offset_attribute(styles={}) click to toggle source

NSBaselineOffsetAttributeName

:baseline_offset

* 0, :default
* A Float
# File lib/motion-mastr/attributes/baseline_offset_attribute.rb, line 10
def baseline_offset_attribute(styles={})
  return nil if styles.nil?
  return nil if styles[:baseline_offset].nil?
  value = styles[:baseline_offset]
  return 0 if [0, :default].include?(value)
  return nil unless value.respond_to?(:to_f)
  return value.to_f
end
expansion_attribute(styles={}) click to toggle source

NSExpansionAttributeName

:expansion

* 0, :default
* A Float
# File lib/motion-mastr/attributes/expansion_attribute.rb, line 10
def expansion_attribute(styles={})
  return nil if styles.nil?
  return nil if styles[:expansion].nil?
  value = styles[:expansion]
  return 0 if [0, :default].include?(value)
  return nil unless value.respond_to?(:to_f)
  return value.to_f
end
font_attribute(styles={}) click to toggle source

NSFontAttributeName

:font = UIFont

# File lib/motion-mastr/attributes/font_attribute.rb, line 8
def font_attribute(styles={})
  return nil if styles.nil?
  value = styles[:font]
  return nil unless value && value.is_a?(UIFont)
  value
end
foreground_color_attribute(styles={}) click to toggle source

NSForegroundColorAttributeName

:foreground_color = UIColor

# File lib/motion-mastr/attributes/foreground_color_attribute.rb, line 8
def foreground_color_attribute(styles={})
  return nil if styles.nil?
  value = styles[:foreground_color]
  value ||= styles[:color]
  return nil unless value && value.is_a?(UIColor)
  value
end
kern_attribute(styles={}) click to toggle source

NSKernAttributeName

:kern or :kerning

* 0, :off, :none, :default
* A Float
# File lib/motion-mastr/attributes/kern_attribute.rb, line 10
def kern_attribute(styles={})
  return nil if styles.nil?
  return unless styles[:kern] || styles[:kerning]
  value = styles[:kern] || styles[:kerning]
  return 0 if [0, :off, :none, :default].include?(value)
  return value.to_f
end
ligature_attribute(styles={}) click to toggle source

NSLigatureAttributeName

:ligature

= 0, :off, :none
= 1, :on, :default
# File lib/motion-mastr/attributes/ligature_attribute.rb, line 10
def ligature_attribute(styles={})
  return nil if styles.nil?
  value = styles[:ligature]
  return 0 if [0, :off, :none].include?(value)
  return 1 if [1, :on, :default].include?(value)
  nil
end
obliqueness_attribute(styles={}) click to toggle source

NSObliquenessAttributeName

:obliqueness

* 0, :default
* A Float
# File lib/motion-mastr/attributes/obliqueness_attribute.rb, line 10
def obliqueness_attribute(styles={})
  return nil if styles.nil?
  return nil if styles[:obliqueness].nil?
  value = styles[:obliqueness]
  return 0 if [0, :default].include?(value)
  return nil unless value.respond_to?(:to_f)
  return value.to_f
end
paragraph_style_attribute(styles={}) click to toggle source

NSParagraphStyleAttributeName

:alignment

:left, :right, :center, :centered

:first_line_head_indent

non-negative Float

:head_indent

non-negative Float

:tail_indent

non-negative Float

:line_break_mode

:by_word, :by_word_wrap, :by_word_wrapping, :word, :word_wrapping, :word_wrap
:by_char, :by_char_wrapping, :char
:clip, :clipping, :by_clipping
:by_truncating_head, :head
:by_truncating_tail, :tail
:by_truncating_middle, :middle

:maximum_line_height

0, :off, :infinite, :none
or a Float

:minimum_line_height

0, :off, :infinite, :none
or a Float

:line_spacing

0 or a positive Float

:paragraph_spacing

0 or a positive Float

:paragraph_spacing_before

0, :default
or a positive Float

:base_writing_direction

:natural, :default
:left, :left_to_right, :l2r
:right, :right_to_left, :r2l

:line_height_multiple

0, :default
or a positive Float

:default_tab_interval

0, :default
or a positive Float

:hyphenation_factor

0, :deffault
or a positive Float up to 1.0
# File lib/motion-mastr/attributes/paragraph_style_attribute.rb, line 70
def paragraph_style_attribute(styles={})
  return nil if styles.nil?

  # search for valid paragraph-related keys
  style_keys                 = styles.keys
  paragraph_related_keys     = [
    :alignment,
    :first_line_head_indent,
    :head_indent,
    :tail_indent,
    :line_break_mode,
    :maximum_line_height,
    :minimum_line_height,
    :line_spacing,
    :paragraph_spacing,
    :paragraph_spacing_before,
    :base_writing_direction,
    :line_height_multiple,
    :default_tab_interval,
    :hyphenation_factor,
  ]
  contains_paragraph_styling = (style_keys - paragraph_related_keys).length != style_keys.length
  return nil unless contains_paragraph_styling

  # start with logical defaults
  paragraph = NSParagraphStyle.defaultParagraphStyle.mutableCopy

  # alignment
  if styles[:alignment]
    case styles[:alignment]
    when :right then paragraph.alignment = NSTextAlignmentRight
    when :center, :centered, :centre, :centred then paragraph.alignment = NSTextAlignmentCenter
    when :left then paragraph.alignment = NSTextAlignmentLeft
    end
  end

  # first line head indent
  if styles[:first_line_head_indent]
    indent = styles[:first_line_head_indent].to_f
    indent = 0 if indent < 0
    paragraph.firstLineHeadIndent = indent
  end

  # head indent
  if styles[:head_indent]
    indent = styles[:head_indent].to_f
    indent = 0 if indent < 0
    paragraph.headIndent = indent
  end

  # tail indent
  if styles[:tail_indent]
    indent = styles[:tail_indent].to_f
    indent = 0 if indent < 0
    paragraph.tailIndent = indent
  end

  # line break mode
  if styles[:line_break_mode]
    case styles[:line_break_mode]
    when :by_word, :by_word_wrap, :by_word_wrapping, :word, :word_wrapping, :word_wrap then paragraph.lineBreakMode = NSLineBreakByWordWrapping
    when :by_char, :by_char_wrapping, :char then paragraph.lineBreakMode = NSLineBreakByCharWrapping
    when :clip, :clipping, :by_clipping then paragraph.lineBreakMode = NSLineBreakByClipping
    when :by_truncating_head, :head then paragraph.lineBreakMode = NSLineBreakByTruncatingHead
    when :by_truncating_tail, :tail then paragraph.lineBreakMode = NSLineBreakByTruncatingTail
    when :by_truncating_middle, :middle then paragraph.lineBreakMode = NSLineBreakByTruncatingMiddle
    end
  end

  # maximum line height
  if styles[:maximum_line_height]
    maximum_line_height = styles[:maximum_line_height]
    maximum_line_height = 0 if [:off, :infinite, 0, :none].include?(maximum_line_height)
    maximum_line_height ||= maximum_line_height.to_f
    maximum_line_height = 0 if maximum_line_height < 0
    paragraph.maximumLineHeight = maximum_line_height
  end

  # minimum line height
  if styles[:minimum_line_height]
    minimum_line_height = styles[:minimum_line_height]
    minimum_line_height = 0 if [:off, :infinite, 0, :none].include?(minimum_line_height)
    minimum_line_height ||= minimum_line_height.to_f
    minimum_line_height = 0 if minimum_line_height < 0
    paragraph.minimumLineHeight = minimum_line_height
  end

  # line spacing
  if styles[:line_spacing]
    line_spacing = styles[:line_spacing].to_f
    line_spacing = 0 if line_spacing < 0
    paragraph.lineSpacing = line_spacing
  end

  # paragraph spacing
  if styles[:paragraph_spacing]
    paragraph_spacing = styles[:paragraph_spacing].to_f
    paragraph_spacing = 0 if paragraph_spacing < 0
    paragraph.paragraphSpacing = paragraph_spacing
  end

  # paragraph spacing before
  if styles[:paragraph_spacing_before]
    paragraph_spacing_before = styles[:paragraph_spacing_before]
    paragraph_spacing_before = 0 if [0, :default].include?(paragraph_spacing_before)
    paragraph_spacing_before ||= styles[:paragraph_spacing_before].to_f
    paragraph_spacing_before = 0 if paragraph_spacing_before < 0
    paragraph.paragraphSpacingBefore = paragraph_spacing_before
  end

  # base writing direction
  if styles[:base_writing_direction]
    case styles[:base_writing_direction]
    when :natural, :default then paragraph.baseWritingDirection = NSWritingDirectionNatural
    when :left, :left_to_right, :l2r then paragraph.baseWritingDirection = NSWritingDirectionLeftToRight
    when :right, :right_to_left, :r2l then paragraph.baseWritingDirection = NSWritingDirectionRightToLeft
    end
  end

  # line height multiple
  if styles[:line_height_multiple]
    line_height_multiple = styles[:line_height_multiple]
    line_height_multiple = 0 if [0, :default].include?(line_height_multiple)
    line_height_multiple ||= styles[:line_height_multiple].to_f
    line_height_multiple = 0 if line_height_multiple < 0
    paragraph.lineHeightMultiple = line_height_multiple
  end

  # default tab interval
  if styles[:default_tab_interval]
    default_tab_interval = styles[:default_tab_interval]
    default_tab_interval = 0 if [0, :default].include?(default_tab_interval)
    default_tab_interval ||= styles[:default_tab_interval].to_f
    default_tab_interval = 0 if default_tab_interval < 0
    paragraph.defaultTabInterval = default_tab_interval
  end

  # hyphenation factor
  if styles[:hyphenation_factor]
    hyphenation_factor = styles[:hyphenation_factor]
    hyphenation_factor = 0 if [0, :default].include?(hyphenation_factor)
    hyphenation_factor ||= styles[:hyphenation_factor].to_f
    hyphenation_factor = 0 if hyphenation_factor < 0
    hyphenation_factor = 1.0 if hyphenation_factor > 1
    paragraph.hyphenationFactor = hyphenation_factor
  end

  paragraph
end
shadow_attribute(styles={}) click to toggle source

NSShadowAttributeName

:shadow_offset

CGSize
# File lib/motion-mastr/attributes/shadow_attribute.rb, line 9
def shadow_attribute(styles={})
  return nil if styles.nil?

  # search for valid shadow-related keys
  style_keys = styles.keys
  shadow_keys = [:shadow_offset, :shadow_blur_radius, :shadow_color]
  contains_shadow_styling = (style_keys - shadow_keys).length != style_keys.length
  return nil unless contains_shadow_styling

  # create our shadow
  shadow = NSShadow.alloc.init

  # offset
  if styles[:shadow_offset]
    offset = styles[:shadow_offset]
    if offset.is_a? CGSize
      shadow.shadowOffset = offset
    elsif offset.is_a? Array
      shadow.shadowOffset = offset if offset.length == 2
    elsif offset.is_a? Fixnum
      shadow.shadowOffset = [offset, offset]
    elsif offset.is_a? Float
      shadow.shadowOffset = [offset, offset]
    end
  end

  # blur
  if styles[:shadow_blur_radius]
    blur = styles[:shadow_blur_radius]
    blur = 0 if [0, :default, :none, :off].include?(blur)
    blur ||= blur.to_f
    blur = 0 if blur < 0
    shadow.shadowBlurRadius = blur
  end

  # color
  shadow.shadowColor = styles[:shadow_color] if styles[:shadow_color].is_a? UIColor

  shadow
end
strikethrough_color_attribute(styles={}) click to toggle source

NSStrikethroughColorAttributeName

:strikethrough_color = UIColor

# File lib/motion-mastr/attributes/strikethrough_color_attribute.rb, line 8
def strikethrough_color_attribute(styles={})
  return nil if styles.nil?
  value = styles[:strikethrough_color]
  return nil unless value && value.is_a?(UIColor)
  value
end
strikethrough_style_attribute(styles={}) click to toggle source

NSStrikethroughStyleAttribute

:strikethrough_style is either a Symbol or Array of symbols which will be OR’d together

# File lib/motion-mastr/attributes/strikethrough_style_attribute.rb, line 8
def strikethrough_style_attribute(styles={})
  return nil if styles.nil?
  value = styles[:strikethrough_style]
  return underline_style_value(value) if value.is_a? Symbol
  if value.is_a? Array
    bitsauce = 0
    value.each { |s| bitsauce |= underline_style_value(s) }
    bitsauce
  else
    nil
  end
end
stroke_color_attribute(styles={}) click to toggle source

NSStrokeColorAttributeName

:stroke_color = UIColor

# File lib/motion-mastr/attributes/stroke_color_attribute.rb, line 8
def stroke_color_attribute(styles={})
  return nil if styles.nil?
  value = styles[:stroke_color]
  return nil unless value && value.is_a?(UIColor)
  value
end
stroke_width_attribute(styles={}) click to toggle source

NSStrokeWidthAttributeName

:stroke_width

* 0, :default
* A Float
# File lib/motion-mastr/attributes/stroke_width_attribute.rb, line 10
def stroke_width_attribute(styles={})
  return nil if styles.nil?
  return nil if styles[:stroke_width].nil?
  value = styles[:stroke_width]
  return 0 if [0, :default].include?(value)
  return nil unless value.respond_to?(:to_f)
  return value.to_f
end
text_effect_attribute(styles={}) click to toggle source

NSTextEffectAttributeName

:text_effect = :symbol

# File lib/motion-mastr/attributes/text_effect_attribute.rb, line 8
def text_effect_attribute(styles={})
  return nil if styles.nil?
  return NSTextEffectLetterpressStyle if [:letterpress, :letterpress_style].include?(styles[:text_effect])
  nil
end
underline_color_attribute(styles={}) click to toggle source

NSUnderlineColorAttributeName

:underline_color = UIColor

# File lib/motion-mastr/attributes/underline_color_attribute.rb, line 8
def underline_color_attribute(styles={})
  return nil if styles.nil?
  value = styles[:underline_color]
  return nil unless value && value.is_a?(UIColor)
  value
end
underline_style_attribute(styles={}) click to toggle source

NSUnderlineStyleAttribute

:underline_style is either a Symbol or Array of symbols which will be OR’d together

# File lib/motion-mastr/attributes/underline_style_attribute.rb, line 8
def underline_style_attribute(styles={})
  return nil if styles.nil?
  value = styles[:underline_style]
  return underline_style_value(value) if value.is_a? Symbol
  if value.is_a? Array
    bitsauce = 0
    value.each { |s| bitsauce |= underline_style_value(s) }
    bitsauce
  else
    nil
  end
end
underline_style_value(value) click to toggle source
# File lib/motion-mastr/attributes/underline_style_helper.rb, line 4
def underline_style_value(value)
  case value
  when :style_none, :none then NSUnderlineStyleNone
  when :style_single, :single then NSUnderlineStyleSingle
  when :style_thick, :thick then NSUnderlineStyleThick
  when :style_double, :double then NSUnderlineStyleDouble
  when :pattern_solid, :solid then NSUnderlinePatternSolid
  when :pattern_dot, :dot then NSUnderlinePatternDot
  when :pattern_dash, :dash then NSUnderlinePatternDash
  when :pattern_dash_dot, :dash_dot then NSUnderlinePatternDashDot
  when :pattern_dash_dot_dot, :dash_dot_dot then NSUnderlinePatternDashDotDot
  when :by_word then NSUnderlineByWord
  else NSUnderlineStyleNone
  end
end