Revision: 749 http://trac.macosforge.org/projects/ruby/changeset/749 Author: ben@tanjero.com Date: 2008-11-20 20:15:20 -0800 (Thu, 20 Nov 2008) Log Message: ----------- Lots of changes on attributed strings Modified Paths: -------------- MacRuby/trunk/lib/hotcocoa/attributed_string.rb Modified: MacRuby/trunk/lib/hotcocoa/attributed_string.rb =================================================================== --- MacRuby/trunk/lib/hotcocoa/attributed_string.rb 2008-11-20 19:36:51 UTC (rev 748) +++ MacRuby/trunk/lib/hotcocoa/attributed_string.rb 2008-11-21 04:15:20 UTC (rev 749) @@ -1,14 +1,43 @@ module HotCocoa class NSRangedProxyAttributeHash + ATTRIBUTE_KEYS = { :font => NSFontAttributeName, + :paragraph_style => NSParagraphStyleAttributeName, + :color => NSForegroundColorAttributeName, + :underline_style => NSUnderlineStyleAttributeName, + :superscript => NSSuperscriptAttributeName, + :background_color => NSBackgroundColorAttributeName, + :attachment => NSAttachmentAttributeName, + :ligature => NSLigatureAttributeName, + :baseline_offset => NSBaselineOffsetAttributeName, + :kerning => NSKernAttributeName, + :link => NSLinkAttributeName, + :stroke_width => NSStrokeWidthAttributeName, + :stroke_color => NSStrokeColorAttributeName, + :underline_color => NSUnderlineColorAttributeName, + :strikethrough_style => NSStrikethroughStyleAttributeName, + :strikethrough_color => NSStrikethroughColorAttributeName, + :shadow => NSShadowAttributeName, + :obliqueness => NSObliquenessAttributeName, + :expansion_factor => NSExpansionAttributeName, + :cursor => NSCursorAttributeName, + :tool_tip => NSToolTipAttributeName, + :character_shape => NSCharacterShapeAttributeName, + :glyph_info => NSGlyphInfoAttributeName, + :marked_clause_segment => NSMarkedClauseSegmentAttributeName, + :spelling_state => NSSpellingStateAttributeName } + + def initialize(proxy) @proxy = proxy end def [](k) + k = attribute_for_key(k) @proxy.string.attribute(k, atIndex:@proxy.range.first, effectiveRange:nil) end def []=(k,v) + k = attribute_for_key(k) @proxy.string.removeAttribute(k, range:@proxy.range.to_NSRange(@proxy.string.length - 1)) @proxy.string.addAttribute(k, value:v, range:@proxy.range.to_NSRange(@proxy.string.length - 1)) end @@ -17,8 +46,29 @@ attributes.each_pair do |k, v| self[k] = v end + self end alias :merge :<< + + def to_hash + @proxy.string.attributesAtIndex(@proxy.range.first, effectiveRange:nil).inject({}) do |h, pair| + h[key_for_attribute(pair.first)] = pair.last + h + end + end + + def inspect + to_hash.inspect + end + + private + def key_for_attribute(attribute) + (ATTRIBUTE_KEYS.select { |k,v| v == attribute }.first || [attribute]).first + end + + def attribute_for_key(key) + ATTRIBUTE_KEYS[key] || key + end end class NSRangedProxyAttributedString @@ -36,16 +86,21 @@ class String def with_attributes(attributes = {}) - NSMutableAttributedString.alloc.initWithString(self, :attributes => attributes) + attributed_string = NSMutableAttributedString.alloc.initWithString(self) + attributed_string.attributes << attributes + attributed_string end end class Range def to_NSRange(max = nil) + location = first if last == -1 and max - last = max + length = max - first + 1 + else + length = last - first + 1 end - NSRange.new(first, last - first + 1) + NSRange.new(location, length) end end @@ -63,6 +118,12 @@ end end + def +(s) + attributed_string = mutableCopy + attributed_string << s + attributed_string + end + def attributes HotCocoa::NSRangedProxyAttributedString.new(self, 0..-1).attributes end
participants (1)
-
source_changes@macosforge.org