Class: NSString
- 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
- #add_observer(target, action, object = nil) ⇒ Object
- #bold(size = nil) ⇒ Object
- #cvar ⇒ Object
- #document ⇒ Object
- #escape_url ⇒ Object
- #exists? ⇒ Boolean
-
#info_plist ⇒ Object
It's convenient to store a property which is dependent on an environment to Info.plist.
- #italic(size = nil) ⇒ Object
- #ivar ⇒ Object
-
#localized(value = nil, table = nil) ⇒ Object
(also: #_)
This can be called as
"Hello".localized
or"Hello"._
. - #monospace(size = nil) ⇒ Object
- #nsattributedstring(attributes = {}) ⇒ Object (also: #attrd)
-
#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
- #resource_url ⇒ Object
- #setter ⇒ Object
- #uicolor(alpha = nil) ⇒ UIColor
- #uifont(size = nil) ⇒ UIFont
- #uiimage ⇒ UIImage
- #uiimageview ⇒ UIImageView
- #uilabel(font = nil) ⇒ UILabel
- #underline(underline_style = nil) ⇒ Object
- #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 |
#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 |
#cvar ⇒ Object
29 30 31 |
# File 'lib/sugarcube-unholy/ivar.rb', line 29 def cvar "@@#{self}" 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
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
10 11 12 |
# File 'lib/sugarcube/nsstring_files.rb', line 10 def exists? NSFileManager.defaultManager.fileExistsAtPath(self.document) end |
#info_plist ⇒ Object
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 |
#ivar ⇒ Object
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.
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 |
#resource_url ⇒ Object
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 |
#setter ⇒ Object
25 26 27 |
# File 'lib/sugarcube-unholy/ivar.rb', line 25 def setter "#{self}=" end |
#uicolor(alpha = nil) ⇒ UIColor
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
25 26 27 28 |
# File 'lib/sugarcube/nsstring.rb', line 25 def uifont(size=nil) 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 |
#uilabel(font = nil) ⇒ UILabel
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_url ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/sugarcube/nsstring.rb', line 66 def unescape_url CFURLCreateStringByReplacingPercentEscapes( nil, self, "" ) end |