Sha256: f9783b69d900fb7edb07f10888611f81888c5d6b0e5aabb36262ca730510df22

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

class NSString

  def nsurl
    @url ||= NSURL.alloc.initWithString(self)
  end
  
  # @return [UIColor]
  def uicolor(alpha=nil)
    if self[0,1] == '#'
      if self.length == 4
        return (self[1] * 2 + self[2] * 2 + self[3] * 2).to_i(16).uicolor(alpha)
      end
      return self[1..-1].to_i(16).uicolor(alpha)
    end

    img = UIImage.imageNamed(self)
    img && img.uicolor(alpha)
  end

  def cgcolor(alpha=nil)
    uicolor(alpha).CGColor
  end

  # @return [UIImage]
  def uiimage
    UIImage.imageNamed(self).tap do |retval|
      NSLog("No image named #{self}") unless retval
    end
  end

  # @return [UIFont]
  def uifont(size=nil)
    size ||= UIFont.systemFontSize
    UIFont.fontWithName(self, size:size)
  end

  def height(hash={})
    font_size = hash[:size] || 16
    width     = hash[:width] || 320
    font      = UIFont.systemFontOfSize(font_size)
    #size      = self.sizeWithFont(font, constrainedToSize:[width, 10000], lineBreakMode:NSLineBreakByCharWrapping)
    rect      = self.boundingRectWithSize([width, 10000], 
                                           options:NSStringDrawingUsesLineFragmentOrigin, 
                                           attributes:{NSFontAttributeName => font},
                                           context:nil)
    rect.size.height.to_i + 1
  end
  
  # @param font [UIFont] Optional, defaults to UIFont.systemFontOfSize(UIFont.systemFontSize)
  # @return [UILabel]
  def uilabel(hash={})
    font_size = hash[:size] || 16
    color= hash[:color].uicolor || UIColor.blackColor
    left = hash[:l] || hash[:left] || 0
    top  = hash[:t] || hash[:top] || 0

    font = UIFont.systemFontOfSize(font_size)
    size = self.sizeWithFont(font)
    label = UILabel.alloc.initWithFrame([[left, top], size])
    label.text = self
    label.font = font
    label.textColor = color
    # why isn't this just the default!?
    label.backgroundColor = :clear.uicolor
    label
  end

  # @return [UIImageView]
  def uiimageview
    self.uiimage.uiimageview
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cha_work-0.1 lib/cha_work/sugar/nsstring.rb