Class: NSString
- Defined in:
- lib/sugarcube/nsstring.rb,
lib/sugarcube/notifications.rb,
lib/sugarcube/nsstring_files.rb
Instance Method Summary collapse
- #add_observer(target, action, object = nil) ⇒ Object
- #document ⇒ Object
- #escape_url ⇒ Object
- #exists? ⇒ Boolean
-
#localized(value = nil, table = nil) ⇒ Object
(also: #_)
This can be called as
"Hello".localized
or"Hello"._
. -
#nsdata(encoding = NSUTF8StringEncoding) ⇒ NSData
NSData representation encoded using UTF8, or a specified encoding.
- #nsurl ⇒ NSURL
- #post_notification(object = nil, user_info = nil) ⇒ Object
- #remove! ⇒ Object
- #remove_observer(target, object = nil) ⇒ Object
- #resource ⇒ Object
- #resource_exists? ⇒ Boolean
- #uicolor(alpha = nil) ⇒ UIColor
- #uifont(size = UIFont.systemFontSize) ⇒ UIFont
- #uiimage ⇒ UIImage
- #uiimageview ⇒ UIImageView
- #unescape_url ⇒ Object
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 |
#document ⇒ Object
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_url ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/sugarcube/nsstring.rb', line 41 def escape_url CFURLCreateStringByAddingPercentEscapes( nil, self, nil, "!*'();:@&=+$,/?%#[]", CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding) ) end |
#exists? ⇒ Boolean
10 11 12 |
# File 'lib/sugarcube/nsstring_files.rb', line 10 def exists? NSFileManager.defaultManager.fileExistsAtPath(self.document) 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)
.
61 62 63 |
# File 'lib/sugarcube/nsstring.rb', line 61 def localized(value=nil, table=nil) @localized = NSBundle.mainBundle.localizedStringForKey(self, value:value, table:table) end |
#nsdata(encoding = NSUTF8StringEncoding) ⇒ NSData
Returns 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 |
#nsurl ⇒ NSURL
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, user_info=nil) if user_info and not user_info.is_a? Hash raise TypeError("Invalid argument #{user_info.class.name} 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! ⇒ 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 |
#resource ⇒ Object
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
27 28 29 |
# File 'lib/sugarcube/nsstring_files.rb', line 27 def resource_exists? NSFileManager.defaultManager.fileExistsAtPath(self.resource) end |
#uicolor(alpha = nil) ⇒ UIColor
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sugarcube/nsstring.rb', line 30 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 |
#uifont(size = UIFont.systemFontSize) ⇒ UIFont
25 26 27 |
# File 'lib/sugarcube/nsstring.rb', line 25 def uifont(size=UIFont.systemFontSize) UIFont.fontWithName(self, size:size) end |
#uiimage ⇒ UIImage
15 16 17 |
# File 'lib/sugarcube/nsstring.rb', line 15 def uiimage UIImage.imageNamed(self) end |
#uiimageview ⇒ UIImageView
20 21 22 |
# File 'lib/sugarcube/nsstring.rb', line 20 def uiimageview self.uiimage ? self.uiimage.uiimageview : UIImageView.alloc.initWithImage(nil) end |
#unescape_url ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/sugarcube/nsstring.rb', line 51 def unescape_url CFURLCreateStringByReplacingPercentEscapes( nil, self, "" ) end |