Class: MotionPrime::LabelDrawElement

Inherits:
DrawElement show all
Includes:
DrawBackgroundMixin, ElementContentTextMixin, ElementTextMixin
Defined in:
motion-prime/elements/draw/label.rb

Instance Attribute Summary

Attributes inherited from BaseElement

#name, #options, #screen, #section, #styles, #view, #view_class, #view_name

Instance Method Summary collapse

Methods included from DrawBackgroundMixin

#draw_background_in_context

Methods included from ElementTextMixin

#attributed_string, #html_string

Methods included from ElementContentTextMixin

#attributed_text?, #cached_content_height, #cached_content_width, #content_attributed_text, #content_font, #content_height, #content_text, #content_width

Methods inherited from DrawElement

#bind_gesture, #computed_bottom, #computed_frame, #computed_height, #computed_inner_bottom, #computed_inner_left, #computed_inner_right, #computed_inner_top, #computed_left, #computed_max_height, #computed_max_width, #computed_outer_height, #computed_outer_width, #computed_right, #computed_top, #computed_width, factory, #hide, #render!, #show, #update_with_options, #view

Methods included from ElementContentPaddingMixin

#cached_content_outer_height, #cached_content_outer_width, #content_outer_height, #content_outer_width, #content_padding_bottom, #content_padding_height, #content_padding_left, #content_padding_right, #content_padding_top, #content_padding_width

Methods included from FrameCalculatorMixin

#calculate_frome_for

Methods inherited from BaseElement

#add_target, after_render, before_render, #bind_gesture, #compute_options!, #computed_options, factory, #hide, #initialize, #render, #render!, #show, #update_with_options

Methods included from HasClassFactory

#camelize_factory, #class_factory, #low_camelize_factory

Methods included from HasStyleChainBuilder

#build_styles_chain

Methods included from HasNormalizer

#normalize_object, #normalize_options

Constructor Details

This class inherits a constructor from MotionPrime::BaseElement

Instance Method Details

#default_padding_for(side) ⇒ Object



77
78
79
80
# File 'motion-prime/elements/draw/label.rb', line 77

def default_padding_for(side)
  return super unless side.to_s == 'top'
  @padding_top || 0
end

#draw_in(rect) ⇒ Object



40
41
42
# File 'motion-prime/elements/draw/label.rb', line 40

def draw_in(rect)
  draw_in_context(UIGraphicsGetCurrentContext())
end

#draw_in_context(context) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'motion-prime/elements/draw/label.rb', line 44

def draw_in_context(context)
  return if computed_options[:hidden]
  size_to_fit_if_needed
  set_text_position

  draw_background_in_context(context)

  UIGraphicsPushContext(context)
  options = draw_options
  if options[:is_html] || options[:line_spacing] || options[:line_height] || options[:underline]
    prepared_text = options[:is_html] ? html_string(options) : attributed_string(options)

    if options[:inner_rect]
      prepared_text.drawInRect(options[:inner_rect])
    else
      prepared_text.drawAtPoint(options[:top_left_corner])
    end
  else
    # regular string
    prepared_text = options[:text]
    options[:text_color].set
    if options[:inner_rect]
      prepared_text.drawInRect(options[:inner_rect],
        withFont: options[:font],
        lineBreakMode: options[:line_break_mode],
        alignment: options[:text_alignment])
    else
      prepared_text.drawAtPoint(options[:top_left_corner], withFont: options[:font])
    end
  end
  UIGraphicsPopContext()
end

#draw_optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'motion-prime/elements/draw/label.rb', line 8

def draw_options
  options = computed_options
  text = (options[:html] || options[:text]).to_s.gsub(/\A[\n\r]+/, '')
  text_color = (options[:text_color] || :black).uicolor
  font = (options[:font] || :system).uifont

  text_alignment_name = options.fetch(:text_alignment, :left)
  text_alignment = text_alignment_name.uitextalignment
  line_break_mode_name = options.fetch(:line_break_mode, :tail_truncation)
  line_break_mode = line_break_mode_name.uilinebreakmode

  top_left_corner = CGPointMake(computed_inner_left, computed_inner_top)
  if options[:number_of_lines].to_i.zero?
    inner_rect = CGRectMake(*top_left_corner.to_a, computed_width, computed_height)
  end
  super.merge({
    text: text,
    is_html: options[:html].present?,
    text_color: text_color,
    font: font,
    text_alignment_name: text_alignment_name,
    text_alignment: text_alignment,
    line_break_mode_name: line_break_mode_name,
    line_break_mode: line_break_mode,
    line_spacing: options[:line_spacing],
    line_height: options[:line_height],
    underline: options[:underline],
    top_left_corner: top_left_corner,
    inner_rect: inner_rect
  })
end

#set_text_positionObject



90
91
92
93
94
95
96
97
98
# File 'motion-prime/elements/draw/label.rb', line 90

def set_text_position
  if computed_options.slice(:padding_top, :padding_bottom, :padding).values.none?
    computed_options[:width] ||= computed_width
    content_height = cached_content_height
    content_height = computed_outer_height if content_height > computed_outer_height
    @padding_top = (computed_outer_height - content_height)/2
    # @padding_top += 1 unless @padding_top.zero?
  end
end

#size_to_fit_if_neededObject



82
83
84
85
86
87
88
# File 'motion-prime/elements/draw/label.rb', line 82

def size_to_fit_if_needed
  if computed_options[:size_to_fit]
    computed_options[:width] ||= cached_content_outer_width
    computed_options[:height] ||= cached_content_outer_height
    reset_computed_values
  end
end