Class: MPTextView

Inherits:
UITextView
  • Object
show all
Includes:
MotionPrime::SupportKeyValueStore, MotionPrime::SupportPaddingAttribute
Defined in:
motion-prime/support/mp_text_view.rb

Overview

This class have some modifications for UITextView:

  • support padding, padding_left, padding_right options

  • support placeholder, placeholder_color, placeholder_font options

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MotionPrime::SupportPaddingAttribute

#apply_padding, #apply_padding!, #apply_padding?, #padding_bottom, #padding_insets, #padding_left, #padding_right, #padding_top

Methods included from MotionPrime::SupportKeyValueStore

#setValue

Instance Attribute Details

#placeholderObject

Returns the value of attribute placeholder.



7
8
9
# File 'motion-prime/support/mp_text_view.rb', line 7

def placeholder
  @placeholder
end

#placeholderColorObject

Returns the value of attribute placeholderColor.



7
8
9
# File 'motion-prime/support/mp_text_view.rb', line 7

def placeholderColor
  @placeholderColor
end

#placeholderFontObject

Returns the value of attribute placeholderFont.



7
8
9
# File 'motion-prime/support/mp_text_view.rb', line 7

def placeholderFont
  @placeholderFont
end

Class Method Details

.default_padding_leftObject



9
10
11
# File 'motion-prime/support/mp_text_view.rb', line 9

def self.default_padding_left
  5
end

.default_padding_rightObject



13
14
15
# File 'motion-prime/support/mp_text_view.rb', line 13

def self.default_padding_right
  5
end

Instance Method Details

#drawPadding(rect) ⇒ Object



17
18
19
20
21
# File 'motion-prime/support/mp_text_view.rb', line 17

def drawPadding(rect)
  # add padding to UITextView
  self.textContainer.lineFragmentPadding = 0 # left/right
  self.textContainerInset = self.padding_insets
end

#drawPlaceholder(rect) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'motion-prime/support/mp_text_view.rb', line 23

def drawPlaceholder(rect)
  padding = UIEdgeInsetsMake(
    padding_top, padding_left,
    padding_bottom, padding_right
  )
  if self.placeholder && self.text.blank?
    color = self.placeholderColor || :gray.uicolor
    color.setFill
    font = self.placeholderFont || self.font || :system.uifont(16)

    color.setFill
    rect = CGRectMake(
      rect.origin.x + padding_left,
      rect.origin.y + padding_top,
      self.frame.size.width - padding_left,
      self.frame.size.height - padding_top
    )
    placeholder.drawInRect(rect, withFont: font)
  end
end

#drawRect(rect) ⇒ Object



44
45
46
47
48
# File 'motion-prime/support/mp_text_view.rb', line 44

def drawRect(rect)
  drawPadding(rect)
  drawPlaceholder(rect)
  super
end

#initPlaceholderObject



50
51
52
53
54
55
# File 'motion-prime/support/mp_text_view.rb', line 50

def initPlaceholder
  NSNotificationCenter.defaultCenter.addObserver(self,
    selector: :textChanged, name: UITextViewTextDidChangeNotification, object: self
  )
  @shouldDrawPlaceholder = placeholder && self.text.blank?
end

#initWithCoder(aDecoder) ⇒ Object

custom initializer



70
71
72
73
74
75
# File 'motion-prime/support/mp_text_view.rb', line 70

def initWithCoder(aDecoder)
  if super
    initPlaceholder
  end
  self
end

#initWithFrame(frame) ⇒ Object



77
78
79
80
81
82
# File 'motion-prime/support/mp_text_view.rb', line 77

def initWithFrame(frame)
  if super
    initPlaceholder
  end
  self
end

#textChangedObject



57
58
59
# File 'motion-prime/support/mp_text_view.rb', line 57

def textChanged
  updatePlaceholderDraw
end

#updatePlaceholderDrawObject



61
62
63
64
65
66
67
# File 'motion-prime/support/mp_text_view.rb', line 61

def updatePlaceholderDraw
  prev = @shouldDrawPlaceholder
  @shouldDrawPlaceholder = placeholder && self.text.blank?
  if prev != @shouldDrawPlaceholder
    self.setNeedsDisplay
  end
end