0

In a case where I have a multiple custom UITableViewCells or UICollectionViewCells with several labels and UI elements, and maintaining good scroll-speed is essential, will CoreText be a faster solution than TextKit?

Ren P
  • 929
  • 9
  • 20
Adama
  • 1,101
  • 8
  • 26

2 Answers2

3

This can't be answered as is without knowing the details. If you want to know you will have to write both versions of your code and measure which one is faster. Until you done that the answer will always be "It depends".

Best advice would be to start with the higher-level API (Text Kit in this case) and see if the performance is good enough. If yes then forget about this question. If no fire up the profiler and find out where it needs to be improved and maybe come back later with specific questions about how to optimize.

Sven
  • 22,475
  • 4
  • 52
  • 71
3

TextKit is built on top of CoreText. The overhead of using the higher level API is negligible.

In fact, in quite a few cases, the TextKit classes are toll-free bridged to their CoreText equivalents (just like Foundation classes like NSString or NSDictionary are toll-free bridged to their CoreFoundation counterparts). An example: NSParagraphStyle is toll-free bridged to CTParagraphStyle:

NSMutableParagraphStyle *paragraphStyle = 
    [[NSMutableParagraphStyle alloc] init];

paragraphStyle.alignment = NSTextAlignmentRight;

CTTextAlignment alignment;
CTParagraphStyleGetValueForSpecifier((CTParagraphStyleRef)paragraphStyle,
                                     kCTParagraphStyleSpecifierAlignment,
                                     sizeof(CTTextAlignment),
                                     &alignment);

NSAssert(alignment == kCTTextAlignmentRight, @"!!");

In most cases, you will end up writing a whole lot of boilerplate to access the CoreText APIs. TextKit handles a lot of this for you.

Bottom line, use TextKit wherever possible. If you cannot achieve the result using TextKit, use CoreText.

Buzzy
  • 3,618
  • 4
  • 30
  • 41