Module: MotionPrime::ElementContentTextMixin

Includes:
ElementTextMixin
Included in:
ButtonElement, ErrorMessageElement, LabelDrawElement, LabelElement, TextFieldElement, TextViewElement
Defined in:
motion-prime/elements/_content_text_mixin.rb

Instance Method Summary collapse

Methods included from ElementTextMixin

#attributed_string, #extract_attributed_string_options, #html_string

Instance Method Details

#attributed_text?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'motion-prime/elements/_content_text_mixin.rb', line 83

def attributed_text?
  computed_options.slice(:html, :line_spacing, :line_height, :underline, :fragment_color).any? || computed_options[:attributed_text_options]
end

#attributed_text_for_text(text) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'motion-prime/elements/_content_text_mixin.rb', line 24

def attributed_text_for_text(text)
  options = {
    text: text,
    font: content_font,
    line_spacing: computed_options[:line_spacing],
    line_height: computed_options[:line_height]
  }
  computed_options[:html].present? ? html_string(options) : attributed_string(options)
end

#cached_content_heightObject



79
80
81
# File 'motion-prime/elements/_content_text_mixin.rb', line 79

def cached_content_height
  @content_height ||= content_height
end

#cached_content_widthObject



57
58
59
# File 'motion-prime/elements/_content_text_mixin.rb', line 57

def cached_content_width
  @content_width ||= content_width
end

#content_fontObject



10
11
12
# File 'motion-prime/elements/_content_text_mixin.rb', line 10

def content_font
  (is_a?(ButtonElement) ? button_content_font : input_content_font) || :system.uifont
end

#content_heightObject



61
62
63
# File 'motion-prime/elements/_content_text_mixin.rb', line 61

def content_height
  @content_height = height_for_attributed_text(current_attributed_text)
end

#content_textObject



6
7
8
# File 'motion-prime/elements/_content_text_mixin.rb', line 6

def content_text
  is_a?(ButtonElement) ? button_content_text : input_content_text
end

#content_widthObject



34
35
36
# File 'motion-prime/elements/_content_text_mixin.rb', line 34

def content_width
  @content_width = width_for_attributed_text(current_attributed_text)
end

#current_attributed_textObject



14
15
16
17
18
19
20
21
22
# File 'motion-prime/elements/_content_text_mixin.rb', line 14

def current_attributed_text
  if view.try(:is_a?, UITextView) && view.text.present?
    text = view.attributedText
    text += ' ' if text.to_s.end_with?("\n") # does not respect \n at the end by default
    text
  else
    attributed_text_for_text(content_text)
  end
end

#height_for_attributed_text(attributed_text) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'motion-prime/elements/_content_text_mixin.rb', line 69

def height_for_attributed_text(attributed_text)
  min, max = computed_options[:min_height].to_f, computed_options[:max_height]
  unless computed_options[:width]
    Prime.logger.error "Please set element width for content height calculation (`#{self.name}` in section `#{section.try(:name)}`)"
  end
  return min if attributed_text.to_s.blank?
  rect = get_content_rect(attributed_text, computed_options[:width] - content_padding_width)
  [[rect.size.height.ceil, max].compact.min, min].max.ceil
end

#height_for_text(text) ⇒ Object



65
66
67
# File 'motion-prime/elements/_content_text_mixin.rb', line 65

def height_for_text(text)
  height_for_attributed_text(attributed_text_for_text(text))
end

#multiline_content_widthObject



38
39
40
41
42
43
# File 'motion-prime/elements/_content_text_mixin.rb', line 38

def multiline_content_width
  unless computed_options[:width]
    Prime.logger.error "Please set element width for multiline content width calculation (`#{self.name}` in section `#{section.try(:name)}`)"
  end
  width_for_attributed_text(current_attributed_text, computed_options[:width] - content_padding_width)
end

#width_for_attributed_text(attributed_text, width = Float::MAX) ⇒ Object



49
50
51
52
53
54
55
# File 'motion-prime/elements/_content_text_mixin.rb', line 49

def width_for_attributed_text(attributed_text, width = Float::MAX)
  min, max = computed_options[:min_width].to_f, computed_options[:max_width]
  return min if attributed_text.to_s.blank?

  rect = get_content_rect(attributed_text, width)
  [[rect.size.width.ceil, max].compact.min, min].max.ceil
end

#width_for_text(text) ⇒ Object



45
46
47
# File 'motion-prime/elements/_content_text_mixin.rb', line 45

def width_for_text(text)
  width_for_attributed_text(attributed_text_for_text(text))
end