Class: NSString
- Defined in:
- lib/all/sugarcube-unholy/ivar.rb,
lib/ios/sugarcube-ui/nsstring.rb,
lib/osx/sugarcube-ui/nsstring.rb,
lib/ios/sugarcube-color/nsstring.rb,
lib/osx/sugarcube-color/nsstring.rb,
lib/cocoa/sugarcube-files/nsstring.rb,
lib/cocoa/sugarcube-base64/nsstring.rb,
lib/cocoa/sugarcube-nsdata/nsstring.rb,
lib/cocoa/sugarcube-nsdate/nsstring.rb,
lib/cocoa/sugarcube-numbers/nsstring.rb,
lib/cocoa/sugarcube-localized/nsstring.rb,
lib/cocoa/sugarcube-foundation/nsstring.rb,
lib/cocoa/sugarcube-files/nsstring_deprecated.rb,
lib/cocoa/sugarcube-notifications/notifications.rb,
lib/ios/sugarcube-attributedstring/nsattributedstring.rb,
lib/osx/sugarcube-attributedstring/nsattributedstring.rb,
lib/cocoa/sugarcube-attributedstring/nsattributedstring.rb
Class Method Summary collapse
-
.from_base64(base64_str, enc = NSUTF8StringEncoding) ⇒ Object
factory method to convert a base64 string into NSString, using UTF8 or a user specified encoding.
Instance Method Summary collapse
- #add_observer(target, action, object = nil) ⇒ Object
-
#app_support ⇒ Object
This method will be removed in iOS 9.
- #app_support_path ⇒ Object
- #attrd(attributes = {}) ⇒ Object
- #attributed_html ⇒ Object
- #bold(size = nil) ⇒ Object
-
#cache ⇒ Object
This method will be removed in iOS 9.
- #cache_path ⇒ Object
- #cgcolor(alpha = nil) ⇒ Object
- #cvar ⇒ Object
-
#document ⇒ Object
This method will be removed in iOS 9.
- #document_path ⇒ Object
- #escape_url ⇒ Object
-
#exists? ⇒ Boolean
This method will be removed in iOS 9.
- #file_exists? ⇒ Boolean
- #file_url ⇒ Object
- #fileurl(baseURL = nil) ⇒ NSURL
-
#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
-
#nan?(style = NSNumberFormatterDecimalStyle) ⇒ Boolean
Boolean.
- #nsattributedstring(attributes = {}) ⇒ Object
- #nscolor(alpha = nil) ⇒ NSColor
-
#nsdata(encoding = NSUTF8StringEncoding) ⇒ NSData
NSData representation encoded using UTF8, or a specified encoding.
-
#nsdate ⇒ Object
checks ISO8601 formats before falling back on natural language detection.
- #nsfont(size = nil) ⇒ NSFont
- #nsimage ⇒ NSImage
- #nsimageview ⇒ NSImageView
- #nslabel(font = nil) ⇒ NSLabel
- #nstimezone ⇒ Object
- #nsurl(baseURL = nil) ⇒ NSURL
- #post_notification(object = nil, user_info = nil) ⇒ Object
-
#remove! ⇒ Object
This method will be removed in iOS 9.
- #remove_accents ⇒ Object
- #remove_file! ⇒ Object
- #remove_observer(target, object = nil) ⇒ Object
-
#resource ⇒ Object
This method will be removed in iOS 9.
- #resource_exists? ⇒ Boolean
- #resource_path ⇒ Object
- #resource_url ⇒ Object
- #setter ⇒ Object
- #skcolor(alpha = nil) ⇒ Object
-
#temporary ⇒ Object
This method will be removed in iOS 9.
- #temporary_path ⇒ Object
-
#to_base64(enc = NSUTF8StringEncoding) ⇒ Object
converts an NSString instance into a base64 string, using UTF8 encoding or a user specified encoding.
- #to_number(style = NSNumberFormatterDecimalStyle) ⇒ 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
Class Method Details
.from_base64(base64_str, enc = NSUTF8StringEncoding) ⇒ Object
factory method to convert a base64 string into NSString, using UTF8 or a user specified encoding.
15 16 17 18 |
# 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 |
Instance Method Details
#add_observer(target, action, object = nil) ⇒ Object
24 25 26 27 28 29 |
# 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 ⇒ Object
This method will be removed in iOS 9
16 17 18 19 |
# 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 ⇒ Object
17 18 19 20 21 22 |
# 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 = {}) ⇒ Object
7 8 9 |
# File 'lib/cocoa/sugarcube-attributedstring/nsattributedstring.rb', line 7 def attrd(attributes={}) self.nsattributedstring(attributes) end |
#attributed_html ⇒ Object
11 12 13 14 |
# File 'lib/cocoa/sugarcube-attributedstring/nsattributedstring.rb', line 11 def attributed_html = {NSDocumentTypeDocumentAttribute => NSHTMLTextDocumentType} NSAttributedString.alloc.initWithData(self.dataUsingEncoding(NSUTF8StringEncoding), options:, documentAttributes:nil, error:nil) end |
#bold(size = nil) ⇒ Object
3 4 5 |
# File 'lib/ios/sugarcube-attributedstring/nsattributedstring.rb', line 3 def bold(size=nil) nsattributedstring.bold(size) end |
#cache ⇒ Object
This method will be removed in iOS 9
10 11 12 13 |
# 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 ⇒ Object
10 11 12 13 14 15 |
# 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) ⇒ Object
16 17 18 |
# File 'lib/ios/sugarcube-color/nsstring.rb', line 16 def cgcolor(alpha=nil) uicolor(alpha).CGColor end |
#cvar ⇒ Object
29 30 31 |
# File 'lib/all/sugarcube-unholy/ivar.rb', line 29 def cvar "@@#{self}" end |
#document ⇒ Object
This method will be removed in iOS 9
4 5 6 7 |
# 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 ⇒ Object
3 4 5 6 7 8 |
# 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 ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/cocoa/sugarcube-foundation/nsstring.rb', line 17 def escape_url CFURLCreateStringByAddingPercentEscapes( nil, self, nil, "!*'();:@&=+$,/?%#[]", CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding) ) end |
#exists? ⇒ Boolean
This method will be removed in iOS 9
28 29 30 31 |
# 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? ⇒ Boolean
31 32 33 34 |
# 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 ⇒ Object
60 61 62 |
# File 'lib/cocoa/sugarcube-files/nsstring.rb', line 60 def file_url NSURL.fileURLWithPath(self) end |
#fileurl(baseURL = nil) ⇒ NSURL
11 12 13 14 15 |
# 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 ⇒ 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 <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
82 83 84 |
# File 'lib/cocoa/sugarcube-files/nsstring.rb', line 82 def info_plist NSBundle.mainBundle.infoDictionary.valueForKey self end |
#italic(size = nil) ⇒ Object
7 8 9 |
# File 'lib/ios/sugarcube-attributedstring/nsattributedstring.rb', line 7 def italic(size=nil) nsattributedstring.italic(size) end |
#ivar ⇒ Object
21 22 23 |
# File 'lib/all/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/cocoa/sugarcube-localized/nsstring.rb', line 5 def localized(value=nil, table=nil) NSBundle.mainBundle.localizedStringForKey(self, value: value, table: table) end |
#monospace(size = nil) ⇒ Object
11 12 13 |
# File 'lib/ios/sugarcube-attributedstring/nsattributedstring.rb', line 11 def monospace(size=nil) nsattributedstring.monospace(size) end |
#nan?(style = NSNumberFormatterDecimalStyle) ⇒ Boolean
Returns boolean.
4 5 6 |
# File 'lib/cocoa/sugarcube-numbers/nsstring.rb', line 4 def nan?(style=NSNumberFormatterDecimalStyle) self.to_number(style).nil? end |
#nsattributedstring(attributes = {}) ⇒ Object
3 4 5 |
# File 'lib/cocoa/sugarcube-attributedstring/nsattributedstring.rb', line 3 def nsattributedstring(attributes={}) NSAttributedString.alloc.initWithString(self, attributes: attributes) end |
#nscolor(alpha = nil) ⇒ NSColor
4 5 6 7 8 9 10 11 12 13 14 |
# 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) ⇒ NSData
Returns NSData representation encoded using UTF8, or a specified encoding.
5 6 7 |
# File 'lib/cocoa/sugarcube-nsdata/nsstring.rb', line 5 def nsdata(encoding=NSUTF8StringEncoding) dataUsingEncoding(encoding) end |
#nsdate ⇒ Object
checks ISO8601 formats before falling back on natural language detection
4 5 6 |
# File 'lib/cocoa/sugarcube-nsdate/nsstring.rb', line 4 def nsdate SugarCube::DateParser.iso8601(self) || SugarCube::DateParser.parse_date(self) end |
#nsfont(size = nil) ⇒ NSFont
11 12 13 14 |
# File 'lib/osx/sugarcube-ui/nsstring.rb', line 11 def nsfont(size=nil) size ||= NSFont.systemFontSize NSFont.fontWithName(self, size: size) end |
#nsimage ⇒ NSImage
4 5 6 7 8 |
# 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 ⇒ NSImageView
32 33 34 35 36 37 38 39 |
# 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) ⇒ NSLabel
18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# 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) ⇒ NSURL
4 5 6 7 8 |
# 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) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# 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! ⇒ Object
This method will be removed in iOS 9
34 35 36 37 |
# 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 ⇒ Object
31 32 33 34 35 36 37 |
# 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! ⇒ Object
36 37 38 39 40 41 |
# 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) ⇒ Object
31 32 33 |
# 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 ⇒ Object
This method will be removed in iOS 9
40 41 42 43 |
# 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? ⇒ Boolean
50 51 52 |
# File 'lib/cocoa/sugarcube-files/nsstring.rb', line 50 def resource_exists? self.resource_path.file_exists? end |
#resource_path ⇒ Object
43 44 45 46 47 48 |
# 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 ⇒ Object
54 55 56 57 58 |
# 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 ⇒ Object
25 26 27 |
# File 'lib/all/sugarcube-unholy/ivar.rb', line 25 def setter "#{self}=" end |
#skcolor(alpha = nil) ⇒ Object
20 21 22 |
# File 'lib/ios/sugarcube-color/nsstring.rb', line 20 def skcolor(alpha=nil) uicolor(alpha) end |
#temporary ⇒ Object
This method will be removed in iOS 9
22 23 24 25 |
# 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 ⇒ Object
24 25 26 27 28 29 |
# 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) ⇒ Object
Support NSDataBase64EncodingOptions options - easy to add, but I want
converts an NSString instance into a base64 string, using UTF8 encoding or a user specified encoding. to add specs, too, and a hash format (e.g. line_length: 64 => NSDataBase64Encoding64CharacterLineLength).
8 9 10 11 |
# 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) ⇒ Object
8 9 10 11 12 13 14 15 |
# 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) ⇒ UIColor
4 5 6 7 8 9 10 11 12 13 14 |
# 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) ⇒ UIFont
11 12 13 14 |
# File 'lib/ios/sugarcube-ui/nsstring.rb', line 11 def uifont(size=nil) size ||= UIFont.systemFontSize UIFont.fontWithName(self, size:size) end |
#uiimage ⇒ UIImage
4 5 6 7 8 |
# 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 ⇒ UIImageView
30 31 32 |
# File 'lib/ios/sugarcube-ui/nsstring.rb', line 30 def uiimageview self.uiimage.uiimageview end |
#uilabel(font = nil) ⇒ UILabel
18 19 20 21 22 23 24 25 26 27 |
# 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) ⇒ Object
15 16 17 18 19 20 21 |
# 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 ⇒ Object
27 28 29 |
# File 'lib/cocoa/sugarcube-foundation/nsstring.rb', line 27 def unescape_url self.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding) end |