Class: NSString

Inherits:
Object show all
Defined in:
lib/sugarcube/nsstring.rb,
lib/sugarcube-unholy/ivar.rb,
lib/sugarcube/notifications.rb,
lib/sugarcube/nsstring_files.rb,
lib/sugarcube-attributedstring/nsattributedstring.rb

Instance Method Summary collapse

Instance Method Details

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



15
16
17
18
19
20
# File 'lib/sugarcube/notifications.rb', line 15

def add_observer(target, action, object=nil)
  NSNotificationCenter.defaultCenter.addObserver(target,
          selector: action,
          name: self,
          object: object)
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

#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/nsstring_files.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



56
57
58
59
60
61
62
63
64
# File 'lib/sugarcube/nsstring.rb', line 56

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

#exists?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/sugarcube/nsstring_files.rb', line 10

def exists?
  NSFileManager.defaultManager.fileExistsAtPath(self.document)
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



55
56
57
# File 'lib/sugarcube/nsstring_files.rb', line 55

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



76
77
78
# File 'lib/sugarcube/nsstring.rb', line 76

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

#nsattributedstring(attributes = {}) ⇒ Object Also known as: attrd



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



10
11
12
# File 'lib/sugarcube/nsstring.rb', line 10

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

#nsurlNSURL

Returns:



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

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

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



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/sugarcube/notifications.rb', line 3

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



14
15
16
17
18
# File 'lib/sugarcube/nsstring_files.rb', line 14

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

#remove_observer(target, object = nil) ⇒ Object



22
23
24
# File 'lib/sugarcube/notifications.rb', line 22

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

#resourceObject



20
21
22
23
24
25
# File 'lib/sugarcube/nsstring_files.rb', line 20

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

  @@sugarcube_resources.stringByAppendingPathComponent(self)
end

#resource_exists?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/sugarcube/nsstring_files.rb', line 27

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

#resource_urlObject



31
32
33
34
35
# File 'lib/sugarcube/nsstring_files.rb', line 31

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

#uicolor(alpha = nil) ⇒ UIColor

Returns:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sugarcube/nsstring.rb', line 31

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 = self.uiimage
  img && img.uicolor(alpha)
end

#uifont(size = nil) ⇒ UIFont

Returns:



25
26
27
28
# File 'lib/sugarcube/nsstring.rb', line 25

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

#uiimageUIImage

Returns:



15
16
17
# File 'lib/sugarcube/nsstring.rb', line 15

def uiimage
  UIImage.imageNamed(self)
end

#uiimageviewUIImageView

Returns:

  • (UIImageView)


20
21
22
# File 'lib/sugarcube/nsstring.rb', line 20

def uiimageview
  self.uiimage ? self.uiimage.uiimageview : UIImageView.alloc.initWithImage(nil)
end

#uilabel(font = nil) ⇒ UILabel

Parameters:

  • font (UIFont) (defaults to: nil)

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

Returns:



45
46
47
48
49
50
51
52
53
54
# File 'lib/sugarcube/nsstring.rb', line 45

def uilabel(font=nil)
  font ||= :system.uifont(:label.uifontsize)
  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



66
67
68
69
70
71
72
# File 'lib/sugarcube/nsstring.rb', line 66

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