Sha256: fd0b94627e25e77afccd37cec02a2ebe29e35f626190a382ef1d6c2ca0dd16a5

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

# This class have some modifications for UITextField:
# * support padding, padding_left, padding_right options
# * support placeholder_color, placeholder_font options
motion_require '_key_value_store'
motion_require '_padding_attribute'
motion_require '_control_content_alignment'
class MPTextField < UITextField
  include MotionPrime::SupportKeyValueStore
  include MotionPrime::SupportPaddingAttribute
  include MotionPrime::SupportControlContentAlignment

  attr_accessor :placeholderColor, :placeholderFont, :readonly, :placeholderAlignment

  def self.default_padding_left
    5
  end

  def self.default_padding_right
    5
  end

  # placeholder position
  def textRectForBounds(bounds)
    @_line_height = font.pointSize
    rect = calculate_rect_for(bounds)
    @_line_height = nil
    rect
  end

  # text position
  def editingRectForBounds(bounds)
    @_line_height = self.font.pointSize
    rect = calculate_rect_for(bounds)
    @_line_height = nil
    rect
  end

  def drawPlaceholderInRect(rect)
    font_diff = self.font.pointSize - placeholder_font.pointSize
    rect.origin.y += font_diff/2.0
    color = self.placeholderColor || :gray.uicolor
    color.setFill

    truncation = :tail_truncation.uilinebreakmode
    alignment = (placeholderAlignment || :left.nstextalignment)
    self.placeholder.drawInRect(rect, withFont: placeholder_font, lineBreakMode: truncation, alignment: alignment)
  end

  private
    def placeholder_font
      self.placeholderFont || self.font || :system.uifont(16)
    end

    def calculate_rect_for(bounds)
      height_diff = self.bounds.size.height - (line_height + padding_top + padding_bottom)
      bounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height - height_diff)
      CGRectMake(
        bounds.origin.x + padding_left,
        bounds.origin.y + padding_top,
        bounds.size.width - (padding_left + padding_right),
        bounds.size.height - (padding_top + padding_bottom)
      )
    end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
motion-prime-1.0.7 motion-prime/support/mp_text_field.rb
motion-prime-1.0.6 motion-prime/support/mp_text_field.rb
motion-prime-1.0.5 motion-prime/support/mp_text_field.rb
motion-prime-1.0.4 motion-prime/support/mp_text_field.rb
motion-prime-1.0.3 motion-prime/support/mp_text_field.rb
motion-prime-1.0.2 motion-prime/support/mp_text_field.rb
motion-prime-1.0.1 motion-prime/support/mp_text_field.rb