Sha256: 158cc23319eeb66efcfe6e5b3b3d8561fcb0685c7a49f6d407e7a590f8890a10

Contents?: true

Size: 1.53 KB

Versions: 14

Compression:

Stored size: 1.53 KB

Contents

class NSString

  # @return [NSURL]
  def nsurl
    @url ||= NSURL.alloc.initWithString(self)
  end

  # @return [NSData] NSData representation encoded using UTF8, or a specified
  #   encoding
  def nsdata(encoding=NSUTF8StringEncoding)
    dataUsingEncoding(encoding)
  end

  # @return [UIImage]
  def uiimage
    UIImage.imageNamed(self)
  end

  # @return [UIImageView]
  def uiimageview
    self.uiimage ? self.uiimage.uiimageview : UIImageView.alloc.initWithImage(nil)
  end

  # @return [UIFont]
  def uifont(size=UIFont.systemFontSize)
    UIFont.fontWithName(self, size:size)
  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

    self.uiimage.uicolor(alpha)
  end

  def escape_url
    CFURLCreateStringByAddingPercentEscapes(
            nil,
            self,
            nil,
            "!*'();:@&=+$,/?%#[]",
            CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)
            )
  end

  def unescape_url
    CFURLCreateStringByReplacingPercentEscapes(
            nil,
            self,
            ""
            )
  end

  # This can be called as `"Hello".localized` or `"Hello"._`.  The `str._`
  # syntax is meant to be reminiscent of gettext-style `_(str)`.
  def localized(value=nil, table=nil)
    @localized = NSBundle.mainBundle.localizedStringForKey(self, value:value, table:table)
  end
  alias _ localized

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
sugarcube-0.16.5 lib/sugarcube/nsstring.rb
sugarcube-0.16.2 lib/sugarcube/nsstring.rb
sugarcube-0.16 lib/sugarcube/nsstring.rb
sugarcube-0.15.5 lib/sugarcube/nsstring.rb
sugarcube-0.15.3 lib/sugarcube/nsstring.rb
sugarcube-0.15.0 lib/sugarcube/nsstring.rb
sugarcube-0.14.0 lib/sugarcube/nsstring.rb
sugarcube-0.13.7 lib/sugarcube/nsstring.rb
sugarcube-0.13.5 lib/sugarcube/nsstring.rb
sugarcube-0.13.4 lib/sugarcube/nsstring.rb
sugarcube-0.13.3 lib/sugarcube/nsstring.rb
sugarcube-0.13.2 lib/sugarcube/nsstring.rb
sugarcube-0.13 lib/sugarcube/nsstring.rb
sugarcube-0.12 lib/sugarcube/nsstring.rb