Class: NSString

Inherits:
Object show all
Defined in:
lib/sugarcube-unholy/ivar.rb,
lib/sugarcube-color/nsstring.rb,
lib/sugarcube-files/nsstring.rb,
lib/sugarcube-uikit/nsstring.rb,
lib/sugarcube-nsdata/nsstring.rb,
lib/sugarcube-nsdate/nsstring.rb,
lib/sugarcube-numbers/nsstring.rb,
lib/sugarcube-localized/nsstring.rb,
lib/sugarcube-foundation/nsstring.rb,
lib/sugarcube-notifications/notifications.rb,
lib/sugarcube-attributedstring/nsattributedstring.rb

Instance Method Summary collapse

Instance Method Details

#add_observer(target, action, object = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/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_supportObject



17
18
19
20
21
22
# File 'lib/sugarcube-files/nsstring.rb', line 17

def app_support
  @@sugarcube_app_suppert ||= NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)[0]
  return self if self.hasPrefix(@@sugarcube_app_suppert)

  @@sugarcube_app_suppert.stringByAppendingPathComponent(self)
end

#attrd(attributes = {}) ⇒ Object



27
28
29
# File 'lib/sugarcube-attributedstring/nsattributedstring.rb', line 27

def attrd(attributes={})
  self.nsattributedstring(attributes)
end

#bold(size = nil) ⇒ Object



3
4
5
6
# File 'lib/sugarcube-attributedstring/nsattributedstring.rb', line 3

def bold(size=nil)
  font = :bold.uifont(size)
  nsattributedstring({NSFontAttributeName => font})
end

#cacheObject



10
11
12
13
14
15
# File 'lib/sugarcube-files/nsstring.rb', line 10

def cache
  @@sugarcube_caches ||= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)[0]
  return self if self.hasPrefix(@@sugarcube_caches)

  @@sugarcube_caches.stringByAppendingPathComponent(self)
end

#cvarObject



29
30
31
# File 'lib/sugarcube-unholy/ivar.rb', line 29

def cvar
  "@@#{self}"
end

#documentObject



3
4
5
6
7
8
# File 'lib/sugarcube-files/nsstring.rb', line 3

def document
  @@sugarcube_docs ||= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
  return self if self.hasPrefix(@@sugarcube_docs)

  @@sugarcube_docs.stringByAppendingPathComponent(self)
end

#escape_urlObject



13
14
15
16
17
18
19
20
21
# File 'lib/sugarcube-foundation/nsstring.rb', line 13

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

#exists?Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/sugarcube-files/nsstring.rb', line 24

def exists?
  path = self.hasPrefix('/') ? self : self.document
  NSFileManager.defaultManager.fileExistsAtPath(path)
end

#fileurlNSURL

Returns:



9
10
11
# File 'lib/sugarcube-foundation/nsstring.rb', line 9

def fileurl
  @fileurl ||= NSURL.fileURLWithPath(self)
end

#info_plistObject

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 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

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



71
72
73
# File 'lib/sugarcube-files/nsstring.rb', line 71

def info_plist
    NSBundle.mainBundle.infoDictionary.valueForKey self
end

#italic(size = nil) ⇒ Object



8
9
10
11
# File 'lib/sugarcube-attributedstring/nsattributedstring.rb', line 8

def italic(size=nil)
  font = :italic.uifont(size)
  nsattributedstring({NSFontAttributeName => font})
end

#ivarObject



21
22
23
# File 'lib/sugarcube-unholy/ivar.rb', line 21

def ivar
  "@#{self}"
end

#localized(value = nil, table = nil) ⇒ Object Also known as: _

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



5
6
7
# File 'lib/sugarcube-localized/nsstring.rb', line 5

def localized(value=nil, table=nil)
  @localized = NSBundle.mainBundle.localizedStringForKey(self, value:value, table:table)
end

#monospace(size = nil) ⇒ Object



13
14
15
16
# File 'lib/sugarcube-attributedstring/nsattributedstring.rb', line 13

def monospace(size=nil)
  font = :monospace.uifont(size)
  nsattributedstring({NSFontAttributeName => font})
end

#nan?Boolean

Returns boolean.

Returns:

  • (Boolean)

    boolean



4
5
6
# File 'lib/sugarcube-numbers/nsstring.rb', line 4

def nan?
  self.to_number.nil?
end

#nsattributedstring(attributes = {}) ⇒ Object



23
24
25
# File 'lib/sugarcube-attributedstring/nsattributedstring.rb', line 23

def nsattributedstring(attributes={})
  NSAttributedString.alloc.initWithString(self, attributes: attributes)
end

#nsdata(encoding = NSUTF8StringEncoding) ⇒ NSData

Returns NSData representation encoded using UTF8, or a specified encoding.

Returns:

  • (NSData)

    NSData representation encoded using UTF8, or a specified encoding



5
6
7
# File 'lib/sugarcube-nsdata/nsstring.rb', line 5

def nsdata(encoding=NSUTF8StringEncoding)
  dataUsingEncoding(encoding)
end

#nsdateObject

checks ISO8601 formats before falling back on natural language detection



4
5
6
# File 'lib/sugarcube-nsdate/nsstring.rb', line 4

def nsdate
  SugarCube::DateParser.iso8601(self) || SugarCube::DateParser.parse_date(self)
end

#nstimezoneObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/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

#nsurlNSURL

Returns:



4
5
6
# File 'lib/sugarcube-foundation/nsstring.rb', line 4

def nsurl
  @url ||= NSURL.alloc.initWithString(self)
end

#post_notification(object = nil, user_info = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sugarcube-notifications/notifications.rb', line 12

def post_notification(object=nil, =nil)
  if  and not .is_a? Hash
    raise TypeError("Invalid argument #{.class.name} sent to String.post_notification")
  end

  if 
    NSNotificationCenter.defaultCenter.postNotificationName(self, object:object, userInfo:)
  else
    NSNotificationCenter.defaultCenter.postNotificationName(self, object:object)
  end
end

#remove!Object



29
30
31
32
33
34
# File 'lib/sugarcube-files/nsstring.rb', line 29

def remove!
  ptr = Pointer.new(:id)
  path = self.hasPrefix('/') ? self : self.document
  NSFileManager.defaultManager.removeItemAtPath(path, error:ptr)
  ptr[0]
end

#remove_observer(target, object = nil) ⇒ Object



31
32
33
# File 'lib/sugarcube-notifications/notifications.rb', line 31

def remove_observer(target, object=nil)
  NSNotificationCenter.defaultCenter.removeObserver(target, name:self, object:object)
end

#resourceObject



36
37
38
39
40
41
# File 'lib/sugarcube-files/nsstring.rb', line 36

def resource
  @@sugarcube_resources ||= NSBundle.mainBundle.resourcePath
  return self if self.hasPrefix(@@sugarcube_resources)

  @@sugarcube_resources.stringByAppendingPathComponent(self)
end

#resource_exists?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/sugarcube-files/nsstring.rb', line 43

def resource_exists?
  NSFileManager.defaultManager.fileExistsAtPath(self.resource)
end

#resource_urlObject



47
48
49
50
51
# File 'lib/sugarcube-files/nsstring.rb', line 47

def resource_url
  a = self.split(".")
  ext = a.pop if a.size >= 2
  NSBundle.mainBundle.URLForResource(a.join("."), withExtension:ext)
end

#setterObject



25
26
27
# File 'lib/sugarcube-unholy/ivar.rb', line 25

def setter
  "#{self}="
end

#to_numberObject



8
9
10
11
# File 'lib/sugarcube-numbers/nsstring.rb', line 8

def to_number
  number_formatter = NSNumberFormatter.new
  number_formatter.numberFromString(self)
end

#uicolor(alpha = nil) ⇒ UIColor

Returns:



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/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) ⇒ UIFont

Returns:



11
12
13
14
# File 'lib/sugarcube-uikit/nsstring.rb', line 11

def uifont(size=nil)
  size ||= UIFont.systemFontSize
  UIFont.fontWithName(self, size:size)
end

#uiimageUIImage

Returns:



4
5
6
7
8
# File 'lib/sugarcube-uikit/nsstring.rb', line 4

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

#uiimageviewUIImageView

Returns:

  • (UIImageView)


30
31
32
33
34
35
36
# File 'lib/sugarcube-uikit/nsstring.rb', line 30

def uiimageview
  image = UIImage.imageNamed(self)
  unless image
    NSLog("No image named #{self}")
  end
  UIImageView.alloc.initWithImage(image)
end

#uilabel(font = nil) ⇒ UILabel

Parameters:

  • font (UIFont) (defaults to: nil)

    Optional, defaults to UIFont.systemFontOfSize(UIFont.systemFontSize)

Returns:



18
19
20
21
22
23
24
25
26
27
# File 'lib/sugarcube-uikit/nsstring.rb', line 18

def uilabel(font=nil)
  font ||= :system.uifont(UIFont.labelFontSize)
  size = self.sizeWithFont(font)
  UILabel.alloc.initWithFrame([[0, 0], size]).tap { |label|
    label.text = self
    label.font = font
    # why isn't this just the default!?
    label.backgroundColor = :clear.uicolor
  }
end

#underline(underline_style = nil) ⇒ Object



18
19
20
21
# File 'lib/sugarcube-attributedstring/nsattributedstring.rb', line 18

def underline(underline_style=nil)
  underline_style ||= NSUnderlineStyleSingle
  nsattributedstring({NSUnderlineStyleAttributeName => underline_style})
end

#unescape_urlObject



23
24
25
26
27
28
29
# File 'lib/sugarcube-foundation/nsstring.rb', line 23

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