Skip to content

Instantly share code, notes, and snippets.

@jackrusher
Created November 23, 2012 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackrusher/4133648 to your computer and use it in GitHub Desktop.
Save jackrusher/4133648 to your computer and use it in GitHub Desktop.
NSMutableAttributedString in RubyMotion
# build attributed string to display the card
mutando = NSMutableAttributedString.alloc.initWithString(display_text)
# assign fonts and colors to regions of mutable string
[ /^(.*)$/, NSFontAttributeName, UIFont.fontWithName("Avenir", size: 16), # font for all
/^(.*)$/, NSForegroundColorAttributeName, "#696969".to_color, # color for all
/(^|\s|,)#([a-zA-Z0-9]*)/, NSForegroundColorAttributeName, "#444444".to_color, # tag
"\u00B6", NSForegroundColorAttributeName, "#e3e3e3".to_color # pilcrow
].each_slice(3).map do |rx,attr,style|
display_text.to_enum(:scan, rx)
.map { m=Regexp.last_match; [m.begin(0),(m.end(0)-m.begin(0))] }
.each { |range| mutando.addAttribute(attr,
value: style,
range: range )}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment