Sha256: 2a5fcd83540572dcdef1704bb25fa1fb4db477db7662363548f556d941bc747d

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 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 = placeholder_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)
    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

3 entries across 3 versions & 1 rubygems

Version Path
motion-prime-1.0.0 motion-prime/support/mp_text_field.rb
motion-prime-0.9.9.2 motion-prime/support/mp_text_field.rb
motion-prime-0.9.9.1 motion-prime/support/mp_text_field.rb