I need to color the string like this “atcatagcatggcattcatagcgattcgat”, where each of “a”, “t”, “c”, “g” has own color. I can use NSMutableAttributedString and .addAttribute in a range, so first, I can find ranges of indexes for the characters as strings and then color them. I guess to color the string when I am editing it I can recalculate on fly all the indexes of “a”, “t”, “c”, “g” by using the NSTextDidChangeNotification through the NSNotificationCenter. I would like to know if there is another way to color the string, since for the very long strings (millions or hundreds of thousands of characters) the above approach doesn’t look as a fast method. Maybe something like to color the only visible part of the string in the frame of textview window ?
Asked
Active
Viewed 97 times
0
-
I'd enumerate each character, and use a dictionary with keys "a", "t", etc. with value the NSColor corresponding to apply the style. Not sure that there is a faster way. But indeed, changing only the visible character may optimize the whole thing. – Larme Apr 14 '16 at 12:30
-
@Larme So, if target for coloring is only a visible part, somehow we should get the start index of the first character in the left-up corner of visible area and the last one in the right-down corner of the area. And, question is how. Or, to use in the .selectedRange() and apply it to the whole area as selected (without visible effect of selection) ? – VYT Apr 14 '16 at 13:30
-
1I don't know about OS X development (only iOS), but maybe there: http://stackoverflow.com/questions/8854688/how-to-get-the-range-of-characters-that-are-visible-from-within-textstoragedidp ? – Larme Apr 14 '16 at 13:32
-
@Larme Thanks for the link !! It looks promising, I will try. – VYT Apr 14 '16 at 13:39