class NSString

Public Class Methods

from_base64(base64_str, enc=NSUTF8StringEncoding) click to toggle source

factory method to convert a base64 string into NSString, using UTF8 or a user specified encoding.

# File lib/cocoa/sugarcube-base64/nsstring.rb, line 15
def self.from_base64(base64_str, enc=NSUTF8StringEncoding)
  data = NSData.from_base64(base64_str.dataUsingEncoding(NSASCIIStringEncoding))
  NSString.alloc.initWithData(data, encoding:enc)
end

Public Instance Methods

_(value=nil, table=nil)
Alias for: localized
add_observer(target, action, object=nil) click to toggle source
# File lib/cocoa/sugarcube-notifications/notifications.rb, line 24
def add_observer(target, action, object=nil)
  NSNotificationCenter.defaultCenter.addObserver(target,
    selector: action,
    name: self,
    object: object)
end
app_support() click to toggle source

This method will be removed in iOS 9

# File lib/cocoa/sugarcube-files/nsstring_deprecated.rb, line 16
def app_support
  SugarCube.log('Warning: SugarCube\'s String#app_support method has been deprecated in favor of String#app_support_path')
  self.app_support_path
end
app_support_path() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 17
def app_support_path
  @@sugarcube_app_support ||= NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)[0]
  return self if self.hasPrefix(@@sugarcube_app_support)

  @@sugarcube_app_support.stringByAppendingPathComponent(self)
end
attrd(attributes={}) click to toggle source
# File lib/cocoa/sugarcube-attributedstring/nsattributedstring.rb, line 7
def attrd(attributes={})
  self.nsattributedstring(attributes)
end
attributed_html() click to toggle source
# File lib/cocoa/sugarcube-attributedstring/nsattributedstring.rb, line 11
def attributed_html
  options = {NSDocumentTypeDocumentAttribute => NSHTMLTextDocumentType}
  NSAttributedString.alloc.initWithData(self.dataUsingEncoding(NSUTF8StringEncoding), options:options, documentAttributes:nil, error:nil)
end
bold(size=nil) click to toggle source
# File lib/ios/sugarcube-attributedstring/nsattributedstring.rb, line 3
def bold(size=nil)
  nsattributedstring.bold(size)
end
cache() click to toggle source

This method will be removed in iOS 9

# File lib/cocoa/sugarcube-files/nsstring_deprecated.rb, line 10
def cache
  SugarCube.log('Warning: SugarCube\'s String#cache method has been deprecated in favor of String#cache_path')
  self.cache_path
end
cache_path() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 10
def cache_path
  @@sugarcube_caches ||= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)[0]
  return self if self.hasPrefix(@@sugarcube_caches)

  @@sugarcube_caches.stringByAppendingPathComponent(self)
end
cgcolor(alpha=nil) click to toggle source
# File lib/ios/sugarcube-color/nsstring.rb, line 16
def cgcolor(alpha=nil)
  uicolor(alpha).CGColor
end
cvar() click to toggle source
# File lib/all/sugarcube-unholy/ivar.rb, line 29
def cvar
  "@@#{self}"
end
document() click to toggle source

This method will be removed in iOS 9

# File lib/cocoa/sugarcube-files/nsstring_deprecated.rb, line 4
def document
  SugarCube.log('Warning: SugarCube\'s String#document method has been deprecated in favor of String#document_path')
  self.document_path
end
document_path() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 3
def document_path
  @@sugarcube_docs ||= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
  return self if self.hasPrefix(@@sugarcube_docs)

  @@sugarcube_docs.stringByAppendingPathComponent(self)
end
escape_url() click to toggle source
# File lib/cocoa/sugarcube-foundation/nsstring.rb, line 17
def escape_url
  CFURLCreateStringByAddingPercentEscapes(
          nil,
          self,
          nil,
          "!*'();:@&=+$,/?%#[]",
          CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)
          )
end
exists?() click to toggle source

This method will be removed in iOS 9

# File lib/cocoa/sugarcube-files/nsstring_deprecated.rb, line 28
def exists?
  SugarCube.log('Warning: SugarCube\'s String#exists? method has been deprecated in favor of String#file_exists?')
  self.file_exists?
end
file_exists?() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 31
def file_exists?
  path = self.hasPrefix('/') ? self : self.document_path
  NSFileManager.defaultManager.fileExistsAtPath(path)
end
file_url() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 60
def file_url
  NSURL.fileURLWithPath(self)
end
fileurl(baseURL=nil) click to toggle source

@return [NSURL]

# File lib/cocoa/sugarcube-foundation/nsstring.rb, line 11
def fileurl(baseURL=nil)
  @fileurl ||= begin
    baseURL.nil? ? NSURL.fileURLWithPath(self) : NSURL.fileURLWithPath(self, relativeToURL: baseURL)
  end
end
info_plist() click to toggle source

It's convenient to store a property which is dependent on an environment to Info.plist. For instance, to use a different server between development and release versions.

In Rakefile <code> app.release do

app.info_plist['VerifyURL'] = "https://buy.itunes.apple.com/verifyReceipt"

end app.development do

app.info_plist['VerifyURL'] = "https://sandbox.itunes.apple.com/verifyReceipt"

end </code>

You can easily get this value at run time like this: 'VerifyURL'.info_plist

# File lib/cocoa/sugarcube-files/nsstring.rb, line 82
def info_plist
  NSBundle.mainBundle.infoDictionary.valueForKey self
end
italic(size=nil) click to toggle source
# File lib/ios/sugarcube-attributedstring/nsattributedstring.rb, line 7
def italic(size=nil)
  nsattributedstring.italic(size)
end
ivar() click to toggle source
# File lib/all/sugarcube-unholy/ivar.rb, line 21
def ivar
  "@#{self}"
end
localized(value=nil, table=nil) click to toggle source

This can be called as `“Hello”.localized` or `“Hello”._`. The `str._` syntax is meant to be reminiscent of gettext-style `_(str)`.

# File lib/cocoa/sugarcube-localized/nsstring.rb, line 5
def localized(value=nil, table=nil)
  NSBundle.mainBundle.localizedStringForKey(self, value: value, table: table)
end
Also aliased as: _
monospace(size=nil) click to toggle source
# File lib/ios/sugarcube-attributedstring/nsattributedstring.rb, line 11
def monospace(size=nil)
  nsattributedstring.monospace(size)
end
nan?(style=NSNumberFormatterDecimalStyle) click to toggle source

@return boolean

# File lib/cocoa/sugarcube-numbers/nsstring.rb, line 4
def nan?(style=NSNumberFormatterDecimalStyle)
  self.to_number(style).nil?
end
nsattributedstring(attributes={}) click to toggle source
# File lib/cocoa/sugarcube-attributedstring/nsattributedstring.rb, line 3
def nsattributedstring(attributes={})
  NSAttributedString.alloc.initWithString(self, attributes: attributes)
end
nscolor(alpha=nil) click to toggle source

@return [NSColor]

# File lib/osx/sugarcube-color/nsstring.rb, line 4
def nscolor(alpha=nil)
  if self[0,1] == '#'
    if self.length == 4
      return (self[1] * 2 + self[2] * 2 + self[3] * 2).to_i(16).nscolor(alpha)
    end
    return self[1..-1].to_i(16).nscolor(alpha)
  end

  img = NSImage.imageNamed(self)
  img && img.nscolor(alpha)
end
nsdata(encoding=NSUTF8StringEncoding) click to toggle source

@return [NSData] NSData representation encoded using UTF8, or a specified

encoding
# File lib/cocoa/sugarcube-nsdata/nsstring.rb, line 5
def nsdata(encoding=NSUTF8StringEncoding)
  dataUsingEncoding(encoding)
end
nsdate() click to toggle source

checks ISO8601 formats before falling back on natural language detection

# File lib/cocoa/sugarcube-nsdate/nsstring.rb, line 4
def nsdate
  SugarCube::DateParser.iso8601(self) || SugarCube::DateParser.parse_date(self)
end
nsfont(size=nil) click to toggle source

@return [NSFont]

# File lib/osx/sugarcube-ui/nsstring.rb, line 11
def nsfont(size=nil)
  size ||= NSFont.systemFontSize
  NSFont.fontWithName(self, size: size)
end
nsimage() click to toggle source

@return [NSImage]

# File lib/osx/sugarcube-ui/nsstring.rb, line 4
def nsimage
  NSImage.imageNamed(self).tap do |retval|
    NSLog("No image named #{self}") unless retval
  end
end
nsimageview() click to toggle source

@return [NSImageView]

# File lib/osx/sugarcube-ui/nsstring.rb, line 32
def nsimageview
  image_view = NSImageView.alloc.init
  image_view.image = self.nsimage
  frame = image_view.frame
  frame.size = image_view.intrinsicContentSize
  image_view.frame = frame
  return image_view
end
nslabel(font=nil) click to toggle source

@param font [NSFont] Optional, defaults to NSFont.systemFontOfSize(NSFont.systemFontSize) @return [NSLabel]

# File lib/osx/sugarcube-ui/nsstring.rb, line 18
def nslabel(font=nil)
  font ||= :system.nsfont(NSFont.labelFontSize)
  size = self.sizeWithAttributes({ NSFontAttributeName => font })
  label = NSTextField.alloc.initWithFrame([[0, 0], size])
  label.bezeled = false
  label.drawsBackground = false
  label.editable = false
  label.selectable = false
  label.stringValue = self
  label.font = font
  return label
end
nstimezone() click to toggle source
# File lib/cocoa/sugarcube-nsdate/nsstring.rb, line 8
def nstimezone
  case self
  when /([+-]?\d{4})/
    sec = $1[-4,2].to_i * 3600
    NSTimeZone.timeZoneForSecondsFromGMT(sec)
  when /(GMT|UTC)([+-]\d{1,2})?/
    sec = $2 ? $2.to_i * 3600 : 0
    NSTimeZone.timeZoneForSecondsFromGMT(sec)
  else
    NSTimeZone.timeZoneWithName(self)
  end
end
nsurl(baseURL=nil) click to toggle source

@return [NSURL]

# File lib/cocoa/sugarcube-foundation/nsstring.rb, line 4
def nsurl(baseURL=nil)
  @url ||= begin
    baseURL.nil? ? NSURL.alloc.initWithString(self) : NSURL.alloc.initWithString(self, relativeToURL: baseURL)
  end
end
post_notification(object=nil, user_info=nil) click to toggle source
# File lib/cocoa/sugarcube-notifications/notifications.rb, line 12
def post_notification(object=nil, user_info=nil)
  if user_info && ! user_info.is_a?(Hash)
    raise TypeError("Invalid argument #{user_info.class.to_s} sent to String.post_notification")
  end

  if user_info
    NSNotificationCenter.defaultCenter.postNotificationName(self, object: object, userInfo: user_info)
  else
    NSNotificationCenter.defaultCenter.postNotificationName(self, object: object)
  end
end
remove!() click to toggle source

This method will be removed in iOS 9

# File lib/cocoa/sugarcube-files/nsstring_deprecated.rb, line 34
def remove!
  SugarCube.log('Warning: SugarCube\'s String#remove! method has been deprecated in favor of String#remove_file!')
  self.remove_file!
end
remove_accents() click to toggle source
# File lib/cocoa/sugarcube-foundation/nsstring.rb, line 31
def remove_accents
  # removes diacritics by using a lossy conversion
  ascii = self.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)

  # turn it back into a string
  return NSString.alloc.initWithData(ascii, encoding: NSASCIIStringEncoding)
end
remove_file!() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 36
def remove_file!
  ptr = Pointer.new(:id)
  path = self.hasPrefix('/') ? self : self.document_path
  NSFileManager.defaultManager.removeItemAtPath(path, error:ptr)
  ptr[0]
end
remove_observer(target, object=nil) click to toggle source
# File lib/cocoa/sugarcube-notifications/notifications.rb, line 31
def remove_observer(target, object=nil)
  NSNotificationCenter.defaultCenter.removeObserver(target, name: self, object: object)
end
resource() click to toggle source

This method will be removed in iOS 9

# File lib/cocoa/sugarcube-files/nsstring_deprecated.rb, line 40
def resource
  SugarCube.log('Warning: SugarCube\'s String#resource method has been deprecated in favor of String#resource_path')
  self.resource_path
end
resource_exists?() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 50
def resource_exists?
  self.resource_path.file_exists?
end
resource_path() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 43
def resource_path
  @@sugarcube_resources ||= NSBundle.mainBundle.resourcePath
  return self if self.hasPrefix(@@sugarcube_resources)

  @@sugarcube_resources.stringByAppendingPathComponent(self)
end
resource_url() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 54
def resource_url
  a = self.split('.')
  ext = a.pop if a.size >= 2
  NSBundle.mainBundle.URLForResource(a.join("."), withExtension:ext)
end
setter() click to toggle source
# File lib/all/sugarcube-unholy/ivar.rb, line 25
def setter
  "#{self}="
end
skcolor(alpha=nil) click to toggle source
# File lib/ios/sugarcube-color/nsstring.rb, line 20
def skcolor(alpha=nil)
  uicolor(alpha)
end
temporary() click to toggle source

This method will be removed in iOS 9

# File lib/cocoa/sugarcube-files/nsstring_deprecated.rb, line 22
def temporary
  SugarCube.log('Warning: SugarCube\'s String#temporary method has been deprecated in favor of String#temporary_path')
  self.temporary_path
end
temporary_path() click to toggle source
# File lib/cocoa/sugarcube-files/nsstring.rb, line 24
def temporary_path
  @@sugarcube_temporary ||= NSTemporaryDirectory()
  return self if self.hasPrefix(@@sugarcube_temporary)

  @@sugarcube_temporary.stringByAppendingPathComponent(self)
end
to_base64(enc=NSUTF8StringEncoding) click to toggle source

converts an NSString instance into a base64 string, using UTF8 encoding or a user specified encoding. @todo Support NSDataBase64EncodingOptions options - easy to add, but I want to add specs, too, and a hash format (e.g. line_length: 64 => NSDataBase64Encoding64CharacterLineLength).

# File lib/cocoa/sugarcube-base64/nsstring.rb, line 8
def to_base64(enc=NSUTF8StringEncoding)
  encoded = self.dataUsingEncoding(enc)
  encoded.to_base64
end
to_number(style=NSNumberFormatterDecimalStyle) click to toggle source
# File lib/cocoa/sugarcube-numbers/nsstring.rb, line 8
def to_number(style=NSNumberFormatterDecimalStyle)
  if style.respond_to?(:nsnumberstyle)
    style = style.nsnumberstyle
  end
  number_formatter = NSNumberFormatter.new
  number_formatter.numberStyle = style
  number_formatter.numberFromString(self)
end
uicolor(alpha=nil) click to toggle source

@return [UIColor]

# File lib/ios/sugarcube-color/nsstring.rb, line 4
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
uifont(size=nil) click to toggle source

@return [UIFont]

# File lib/ios/sugarcube-ui/nsstring.rb, line 11
def uifont(size=nil)
  size ||= UIFont.systemFontSize
  UIFont.fontWithName(self, size:size)
end
uiimage() click to toggle source

@return [UIImage]

# File lib/ios/sugarcube-ui/nsstring.rb, line 4
def uiimage
  UIImage.imageNamed(self).tap do |retval|
    NSLog("No image named #{self}") unless retval
  end
end
uiimageview() click to toggle source

@return [UIImageView]

# File lib/ios/sugarcube-ui/nsstring.rb, line 30
def uiimageview
  self.uiimage.uiimageview
end
uilabel(font=nil) click to toggle source

@param font [UIFont] Optional, defaults to UIFont.systemFontOfSize(UIFont.systemFontSize) @return [UILabel]

# File lib/ios/sugarcube-ui/nsstring.rb, line 18
def uilabel(font=nil)
  font ||= :system.uifont(UIFont.labelFontSize)
  size = self.sizeWithFont(font)
  label = UILabel.alloc.initWithFrame([[0, 0], size])
  label.text = self
  label.font = font
  # why isn't this just the default!?
  label.backgroundColor = :clear.uicolor
  return label
end
underline(underline_style=nil) click to toggle source
# File lib/ios/sugarcube-attributedstring/nsattributedstring.rb, line 15
def underline(underline_style=nil)
  if underline_style
    nsattributedstring.underline_style(underline_style)
  else
    nsattributedstring.underline
  end
end
unescape_url() click to toggle source
# File lib/cocoa/sugarcube-foundation/nsstring.rb, line 27
def unescape_url
  self.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
end