Module: MotionPrime::ElementTextMixin

Included in:
ElementContentTextMixin, LabelDrawElement, LabelElement, ViewStyler
Defined in:
motion-prime/elements/_text_mixin.rb

Instance Method Summary collapse

Instance Method Details

#attributed_string(options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'motion-prime/elements/_text_mixin.rb', line 31

def attributed_string(options)
  attributes, paragrah_style = extract_attributed_string_options(options)

  prepared_text = NSMutableAttributedString.alloc.initWithString(options[:text].to_s, attributes: attributes)
  underline_range = options[:underline]
  fragment_color = options[:fragment_color]
  fragment_font = options[:fragment_font]
  if paragrah_style && (underline_range || fragment_color) && options.fetch(:number_of_lines, 1) == 1
    Prime.logger.debug "If attributed text has paragraph style and underline - you must set number of lines != 1"
  end

  if underline_range
    underline_range = [0, options[:text].length] if underline_range === true
    prepared_text.addAttributes({NSUnderlineStyleAttributeName => NSUnderlineStyleSingle}, range: underline_range)
  end
  if fragment_color
    prepared_text.addAttributes({NSForegroundColorAttributeName => fragment_color[:color].uicolor}, range: fragment_color[:range])
  end
  if fragment_font
    prepared_text.addAttributes({NSFontAttributeName => fragment_font[:font].uifont}, range: fragment_font[:range])
  end

  prepared_text
end

#extract_attributed_string_options(options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'motion-prime/elements/_text_mixin.rb', line 56

def extract_attributed_string_options(options)
  attributes = {}
  line_height = options[:line_height]
  line_spacing = options[:line_spacing]
  text_alignment = options[:text_alignment]
  line_break_mode = options[:line_break_mode]

  if line_height || line_spacing || text_alignment || line_break_mode
    paragrah_style = NSMutableParagraphStyle.alloc.init
    if line_height
      paragrah_style.setMinimumLineHeight(line_height)
    elsif line_spacing
      paragrah_style.setLineSpacing(line_spacing)
    end
    if text_alignment
      text_alignment = text_alignment.nstextalignment if text_alignment.is_a?(Symbol)
      paragrah_style.setAlignment(text_alignment)
    end
    if line_break_mode
      line_break_mode = line_break_mode.uilinebreakmode if line_break_mode.is_a?(Symbol)
      paragrah_style.setLineBreakMode(line_break_mode)
    end
    attributes[NSParagraphStyleAttributeName] = paragrah_style
  end
  if color = options[:text_color] || options[:title_color]
    attributes[NSForegroundColorAttributeName] = color.uicolor
  end
  if font = extract_font_from(options)
    attributes[NSFontAttributeName] = font.uifont
  end
  [attributes, paragrah_style]
end

#html_string(options) ⇒ Object

Options text text_color font line_spacing text_alignment text_alignment_name line_break_mode underline (range)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'motion-prime/elements/_text_mixin.rb', line 13

def html_string(options)
  styles = []
  styles << "color: #{options[:text_color].hex};" if options[:text_color]
  styles << "line-height: #{options[:line_height] || (options[:line_spacing].to_f + options[:font].pointSize)}px;"
  styles << "font-family: '#{options[:font].familyName}';"
  styles << "font-size: #{options[:font].pointSize}px;"
  styles << "text-align: #{options[:text_alignment_name]};" if options[:text_alignment_name]

  html_options = {
    NSDocumentTypeDocumentAttribute => NSHTMLTextDocumentType,
    NSCharacterEncodingDocumentAttribute => NSNumber.numberWithInt(NSUTF8StringEncoding)
  }

  text = "#{options[:text]}<style>* { #{styles.join} }</style>"
  # WARNING: there is a bug - it uses WebKit and can not be used in secondary threads
  NSAttributedString.alloc.initWithData(text.dataUsingEncoding(NSUTF8StringEncoding), options: html_options, documentAttributes: nil, error: nil)
end