Class: MPTextView

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

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.



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

def placeholder
  @placeholder
end

#placeholderColorObject

Returns the value of attribute placeholderColor.



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

def placeholderColor
  @placeholderColor
end

#placeholderFontObject

Returns the value of attribute placeholderFont.



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

def placeholderFont
  @placeholderFont
end

Class Method Details

.default_padding_leftObject



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

def self.default_padding_left
  5
end

.default_padding_rightObject



16
17
18
# File 'motion-prime/support/mp_text_view.rb', line 16

def self.default_padding_right
  5
end

Instance Method Details

#drawPadding(rect) ⇒ Object



20
21
22
23
24
# File 'motion-prime/support/mp_text_view.rb', line 20

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

#drawPlaceholder(rect) ⇒ Object



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

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



47
48
49
50
51
# File 'motion-prime/support/mp_text_view.rb', line 47

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

#initPlaceholderObject



53
54
55
56
57
58
# File 'motion-prime/support/mp_text_view.rb', line 53

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

#initWithCoder(aDecoder) ⇒ Object

custom initializer



73
74
75
76
77
78
# File 'motion-prime/support/mp_text_view.rb', line 73

def initWithCoder(aDecoder)
  if super
    initPlaceholder
  end
  self
end

#initWithFrame(frame) ⇒ Object



80
81
82
83
84
85
# File 'motion-prime/support/mp_text_view.rb', line 80

def initWithFrame(frame)
  if super
    initPlaceholder
  end
  self
end

#textChangedObject



60
61
62
# File 'motion-prime/support/mp_text_view.rb', line 60

def textChanged
  updatePlaceholderDraw
end

#updatePlaceholderDrawObject



64
65
66
67
68
69
70
# File 'motion-prime/support/mp_text_view.rb', line 64

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