I found that the staining time of strings depends on the number of different NSColors. In the code below, if I use only one color for three cases, the process of coloring the text is 3 times faster than when three different colors are used for these three cases, each color for each case. What for? Is there a way to not slow down the coloring for three different colors?
for i in 0..<arrayOfNSRangesForA.count { textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForA[i]) } for i in 0..<arrayOfNSRangesForT.count { textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForT[i]) } for i in 0..<arrayOfNSRangesForC.count { textFromStorage.addAttribute(NSForegroundColorAttributeName, value: NSColor.green, range: arrayOfNSRangesForC[i]) }
Update I found another OTHER thing. When I changed the color from NSForegroundColorAttributeName to NSBackgroundColorAttributeName , the runtime increased significantly - 10 times. For 20,000 characters, this was for one color, for NSForegroundColorAttributeName - 1 second, for NSBackgroundColorAttributeName - 10 seconds; if three colors - 3 and 30 seconds respectively. For me, this is a very bad feature of Swift !!! Normal DNA staining (ATGC) is not possible because the DNA is thousands of characters A, T, G, C!
Update In the comments, I have a suggestion to color only the visible part of the text. I tried this approach, and it is much worse even for shorter text compared to what I did in a standard way. Thus, I had NSRange of text for the visible part of the text, and the coloring was done on the fly, scrolling through the notification when scrolling. This is bad.
arrays swift nsattributedstring nscolor
Vyt
source share