Class: HotCocoa::NSRangedProxyAttributeHash

Inherits:
Object
  • Object
show all
Defined in:
lib/hotcocoa/attributed_string_helpers.rb

Overview

Similar to a Ruby Hash, except specialized for dealing with a hash of attributes for NSAttributedString objects.

Constant Summary

ATTRIBUTE_KEYS =
{
  font:                  NSFontAttributeName,
  paragraph_style:       NSParagraphStyleAttributeName,
  color:                 NSForegroundColorAttributeName,
  colour:                NSForegroundColorAttributeName,
  underline_style:       NSUnderlineStyleAttributeName,
  superscript:           NSSuperscriptAttributeName,
  background_color:      NSBackgroundColorAttributeName,
  background_colour:     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,
  strikethrough_colour:  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
}

Instance Method Summary collapse

Constructor Details

#initialize(proxy) ⇒ NSRangedProxyAttributeHash



43
44
45
# File 'lib/hotcocoa/attributed_string_helpers.rb', line 43

def initialize proxy
  @proxy = proxy
end

Instance Method Details

#<<(attributes) ⇒ Object Also known as: merge!

Append new attributes to the string



65
66
67
68
# File 'lib/hotcocoa/attributed_string_helpers.rb', line 65

def << attributes
  attributes.each_pair { |k, v| self[k] = v }
  self
end

#[](k) ⇒ Object



47
48
49
50
# File 'lib/hotcocoa/attributed_string_helpers.rb', line 47

def [] k
  k = attribute_for_key k
  @proxy.string.attribute k, atIndex: @proxy.range.first, effectiveRange: nil
end

#[]=(k, v) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/hotcocoa/attributed_string_helpers.rb', line 52

def []= k, v
  k = attribute_for_key k
  @proxy.string.removeAttribute k,
                         range: @proxy.range.relative_to(@proxy.string.length)
  @proxy.string.addAttribute k,
                      value: v,
                      range: @proxy.range.relative_to(@proxy.string.length)
end

#inspectString



83
84
85
# File 'lib/hotcocoa/attributed_string_helpers.rb', line 83

def inspect
  to_hash.inspect
end

#to_hashNSMutableDictionary

Return a hash of the attributes, but without transforming constant names.



76
77
78
79
80
# File 'lib/hotcocoa/attributed_string_helpers.rb', line 76

def to_hash
  dict = @proxy.string.attributesAtIndex @proxy.range.first,
                         effectiveRange: nil
  NSMutableDictionary.dictionaryWithDictionary dict
end