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)


74
75
76
# File 'motion-prime/elements/_content_text_mixin.rb', line 74

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



70
71
72
# File 'motion-prime/elements/_content_text_mixin.rb', line 70

def cached_content_height
  @content_height ||= content_height
end

#cached_content_widthObject



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

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



54
55
56
# File 'motion-prime/elements/_content_text_mixin.rb', line 54

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



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

def height_for_attributed_text(attributed_text)
  min, max = computed_options[:min_height].to_f, computed_options[:max_height]
  return min if attributed_text.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



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

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

#width_for_attributed_text(attributed_text) ⇒ Object



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

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

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

#width_for_text(text) ⇒ Object



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

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