Class: UITextView

Inherits:
Object show all
Defined in:
lib/formotion/patch/ui_text_view.rb,
lib/formotion/patch/ui_text_view_placeholder.rb

Overview

Methods which use blocks for UITextViewDelegate methods. EX field.should_end? do |text_field|

if text_field.text != "secret"
  return false
end
true

end

Also includes an on_change method, which calls after the text has changed (there is no UITextViewDelegate equivalent.) EX field.on_change do |text_field|

p text_field.text

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#placeholderObject

Returns the value of attribute placeholder.



2
3
4
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 2

def placeholder
  @placeholder
end

#placeholder_colorObject

Returns the value of attribute placeholder_color.



3
4
5
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 3

def placeholder_color
  @placeholder_color
end

Instance Method Details

#deallocObject



26
27
28
29
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 26

def dealloc
  NSNotificationCenter.defaultCenter.unobserve @foreground_observer
  old_dealloc
end

#drawRect(rect) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 32

def drawRect(rect)
  old_drawRect(rect)

  if (@shouldDrawPlaceholder)
      self.placeholder_color.set
      self.font ||= UIFont.systemFontOfSize(17) # ios7 seems to have a bug where font of uitextview isn't set by default
      self.placeholder.drawInRect(placeholder_rect, withFont:self.font)
  end
end

#initWithCoder(decoder) ⇒ Object



6
7
8
9
10
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 6

def initWithCoder(decoder)
  old_initWithCoder(decoder)
  setup
  self
end

#initWithFrame(frame) ⇒ Object



13
14
15
16
17
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 13

def initWithFrame(frame)
  old_initWithFrame(frame)
  setup
  self
end

#old_deallocObject



25
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 25

alias_method :old_dealloc, :dealloc

#old_drawRectObject



31
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 31

alias_method :old_drawRect, :drawRect

#old_initWithCoderObject



5
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 5

alias_method :old_initWithCoder, :initWithCoder

#old_initWithFrameObject



12
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 12

alias_method :old_initWithFrame, :initWithFrame

#old_setTextObject



43
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 43

alias_method :old_setText, :setText

#on_begin(&block) ⇒ Object

block takes argument textView



26
27
28
29
30
# File 'lib/formotion/patch/ui_text_view.rb', line 26

def on_begin(&block)
  add_delegate_method do
    @delegate.textViewDidBeginEditing_callback = block
  end
end

#on_change(&block) ⇒ Object

block takes argument textView



47
48
49
50
51
# File 'lib/formotion/patch/ui_text_view.rb', line 47

def on_change(&block)
  add_delegate_method do
    @delegate.textViewDidChange_callback = block
  end
end

#on_end(&block) ⇒ Object

block takes argument textView



40
41
42
43
44
# File 'lib/formotion/patch/ui_text_view.rb', line 40

def on_end(&block)
  add_delegate_method do
    @delegate.textViewDidEndEditing_callback = block
  end
end

#placeholder_rectObject



58
59
60
61
62
63
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 58

def placeholder_rect
  x_offset = font.xHeight
  y_offset = font.capHeight + font.descender

  CGRectMake(self.contentInset.left + x_offset, self.contentInset.top + y_offset, self.frame.size.width - self.contentInset.left - self.contentInset.right - x_offset, self.frame.size.height - self.contentInset.top - self.contentInset.bottom - y_offset)
end

#setText(text) ⇒ Object



44
45
46
47
48
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 44

def setText(text)
  old_setText(text)

  updateShouldDrawPlaceholder
end

#setupObject



19
20
21
22
23
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 19

def setup
  @foreground_observer = NSNotificationCenter.defaultCenter.observe UITextViewTextDidChangeNotification do |notification|
    updateShouldDrawPlaceholder
  end
end

#should_begin?(&block) ⇒ Boolean

block takes argument textView; should return true/false

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/formotion/patch/ui_text_view.rb', line 19

def should_begin?(&block)
  add_delegate_method do
    @delegate.textViewShouldBeginEditing_callback = block
  end
end

#should_change?(&block) ⇒ Boolean

block takes argument textView, range [NSRange], and string; should return true/false

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/formotion/patch/ui_text_view.rb', line 54

def should_change?(&block)
  add_delegate_method do
    @delegate.shouldChangeCharactersInRange_callback = block
  end
end

#should_end?(&block) ⇒ Boolean

block takes argument textView; should return true/false

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/formotion/patch/ui_text_view.rb', line 33

def should_end?(&block)
  add_delegate_method do
    @delegate.textViewShouldEndEditing_callback = block
  end
end

#updateShouldDrawPlaceholderObject



69
70
71
72
73
74
# File 'lib/formotion/patch/ui_text_view_placeholder.rb', line 69

def updateShouldDrawPlaceholder
  prev = @shouldDrawPlaceholder;
  @shouldDrawPlaceholder = self.placeholder && self.text.length == 0

  self.setNeedsDisplay if (prev != @shouldDrawPlaceholder)
end