Created
November 23, 2012 01:42
-
-
Save jackrusher/4133648 to your computer and use it in GitHub Desktop.
NSMutableAttributedString in RubyMotion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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