PageRenderTime 24ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/formotion/patch/ui_text_field.rb

http://github.com/clayallsopp/formotion
Ruby | 166 lines | 112 code | 23 blank | 31 comment | 14 complexity | 1441ef96d4f341399026d9eb79795da2 MD5 | raw file
  1. # Methods which use blocks for UITextFieldDelegate methods.
  2. # EX
  3. # field.should_end? do |text_field|
  4. # if text_field.text != "secret"
  5. # return false
  6. # end
  7. # true
  8. # end
  9. #
  10. # Also includes an on_change method, which calls after the text
  11. # has changed (there is no UITextFieldDelegate equivalent.)
  12. # EX
  13. # field.on_change do |text_field|
  14. # p text_field.text
  15. # end
  16. class UITextField
  17. # block takes argument textField; should return true/false
  18. def should_begin?(&block)
  19. add_delegate_method do
  20. @delegate.textFieldShouldBeginEditing_callback = block
  21. end
  22. end
  23. # block takes argument textField
  24. def on_begin(&block)
  25. add_delegate_method do
  26. @delegate.textFieldDidBeginEditing_callback = block
  27. end
  28. end
  29. # block takes argument textField; should return true/false
  30. def should_end?(&block)
  31. add_delegate_method do
  32. @delegate.textFieldShouldEndEditing_callback = block
  33. end
  34. end
  35. # block takes argument textField
  36. def on_end(&block)
  37. add_delegate_method do
  38. @delegate.textFieldDidEndEditing_callback = block
  39. end
  40. end
  41. # block takes argument textField, range [NSRange], and string; should return true/false
  42. def should_change?(&block)
  43. add_delegate_method do
  44. @delegate.shouldChangeCharactersInRange_callback = block
  45. end
  46. end
  47. # block takes argument textField
  48. def on_change(&block)
  49. add_delegate_method do
  50. @delegate.on_change_callback = block
  51. self.addTarget(@delegate, action: 'on_change:', forControlEvents: UIControlEventEditingChanged)
  52. end
  53. end
  54. # block takes argument textField; should return true/false
  55. def should_clear?(&block)
  56. add_delegate_method do
  57. @delegate.textFieldShouldClear_callback = block
  58. end
  59. end
  60. # block takes argument textField; should return true/false
  61. def should_return?(&block)
  62. add_delegate_method do
  63. @delegate.textFieldShouldReturn_callback = block
  64. end
  65. end
  66. private
  67. def add_delegate_method
  68. # create strong reference to the delegate
  69. # (.delegate= only creates a weak reference)
  70. @delegate ||= UITextField_Delegate.new
  71. yield
  72. self.delegate = @delegate
  73. end
  74. end
  75. class UITextField_Delegate
  76. [:textFieldShouldBeginEditing, :textFieldDidBeginEditing,
  77. :textFieldShouldEndEditing, :textFieldDidEndEditing,
  78. :shouldChangeCharactersInRange, :textFieldShouldClear,
  79. :textFieldShouldReturn].each {|method|
  80. attr_accessor (method.to_s + "_callback").to_sym
  81. }
  82. # Called from
  83. # [textField addTarget:block
  84. # action:'call'
  85. # forControlEvents:UIControlEventEditingChanged],
  86. # NOT a UITextFieldDelegate method.
  87. attr_accessor :on_change_callback
  88. def textFieldShouldBeginEditing(theTextField)
  89. if self.textFieldShouldBeginEditing_callback
  90. return self.textFieldShouldBeginEditing_callback.call(theTextField)
  91. end
  92. true
  93. end
  94. def textFieldDidBeginEditing(theTextField)
  95. if self.textFieldDidBeginEditing_callback
  96. return self.textFieldDidBeginEditing_callback.call(theTextField)
  97. end
  98. end
  99. def textFieldShouldEndEditing(theTextField)
  100. if self.textFieldShouldEndEditing_callback
  101. return self.textFieldShouldEndEditing_callback.call(theTextField)
  102. end
  103. if BW::Device.ios_version >= "7.0"
  104. theTextField.text = theTextField.text.gsub("\u00a0", " ").strip
  105. end
  106. true
  107. end
  108. def textFieldDidEndEditing(theTextField)
  109. if self.textFieldDidEndEditing_callback
  110. return self.textFieldDidEndEditing_callback.call(theTextField)
  111. end
  112. end
  113. def textField(theTextField, shouldChangeCharactersInRange:range, replacementString:string)
  114. if self.shouldChangeCharactersInRange_callback
  115. return self.shouldChangeCharactersInRange_callback.call(theTextField, range, string)
  116. end
  117. # fix for UITextField in iOS7 http://stackoverflow.com/questions/19569688/uitextfield-spacebar-does-not-advance-cursor-in-ios-7/20129483#20129483
  118. if BW::Device.ios_version >= "7.0"
  119. if range.location == theTextField.text.length && string == " "
  120. theTextField.text = theTextField.text.stringByAppendingString("\u00a0")
  121. return false
  122. end
  123. end
  124. true
  125. end
  126. def on_change(theTextField)
  127. if self.on_change_callback
  128. self.on_change_callback.call(theTextField)
  129. end
  130. end
  131. def textFieldShouldClear(theTextField)
  132. if self.textFieldShouldClear_callback
  133. return self.textFieldShouldClear_callback.call(theTextField)
  134. end
  135. true
  136. end
  137. def textFieldShouldReturn(theTextField)
  138. if self.textFieldShouldReturn_callback
  139. return self.textFieldShouldReturn_callback.call(theTextField)
  140. end
  141. true
  142. end
  143. end