Core Text Performance Core Text Performance ios ios

Core Text Performance


You probably should not be using CTFramesetter to create something like UITextView. Instead, you should likely keep an array of CTLine references. If you need help with word breaking, then you can use a CTTypeSetter, but you only need to hand it lines at the current caret and below (you'll still be creating and destroying typesetters a bit, so watch how much you ask of them).

One nice thing about keeping an array of CTLines is that you can throw away the ones you don't need if you're low on memory and reconstruct them later. Just keep track of the character range for each line.


Since my original question, I did some more investigating and found out that the more attributes the drawn string has, the longer it takes.

With that knowledge I decided to simply delete/hide any attributes (specifically kCTForegroundColor) the user could not see, this sped up the drawing ten fold and made it a much more usable experience.


An alternative approach is to continue to use CTFramesetter, but use smaller CTFrames. Just your NSAttributedString into substrings (e.g. using [NSString paragraphRangeForRange:] to get paragraph ranges and then break your attributed string up using attributedSubstringFromRange:). Then create a CTFrame per paragraph. When something changes (e.g. the user types something), you only update the CTFrame(s) that changed.

This means you get to keep taking advantage of what CTFramesetter gives you without the performance penalty of re-setting all the text every time.