Core Graphics text is much faster than Core Text - I feel like I'm missing something

Here is the code that I use to show some text in the main graphic:

int y = MESSAGE_FRAME.origin.y + 8;

if (month) y = y + 27;
int height = [JHomeViewCellContentView heightOfMessage:self.entry.message];

CGRect rect = CGRectMake(MESSAGE_FRAME.origin.x + 8, y, MESSAGE_FRAME.size.width - 16, height);

UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];

[[UIColor colorWithWhite:0.2 alpha:1.0] setFill];

[self.entry.message drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];

And here is the code, if I want to show the same text in the main text:

CGRect rect2 = CGRectMake(MESSAGE_FRAME.origin.x + 8, self.bounds.size.height - y - height + 2, MESSAGE_FRAME.size.width - 16, height);


    UIFont *font = [UIFont fontWithName:@"Crimson" size:15.0f];
    CGFloat lineHeight = [font lineHeight];

    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"Crimson", 15.0f, NULL);

    CGFloat lineSpacing = 0;

    CTParagraphStyleSetting settings[] = {
        { kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(lineHeight), &lineHeight },
        { kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(lineSpacing), &lineSpacing },

    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));

    NSMutableDictionary *attDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   (__bridge_transfer id)fontRef, (NSString *)kCTFontAttributeName,
                                          (id)[[UIColor colorWithWhite:0.2 alpha:1.0] CGColor], (NSString *)kCTForegroundColorAttributeName,
                                          paragraphStyle, kCTParagraphStyleAttributeName,
                                          nil];

     NSAttributedString *attString = [[NSAttributedString alloc] initWithString:self.entry.message attributes:attDictionary];

    //Flip the coordinate system
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);



    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, rect2);


    self.framesetter = CTFramesetterCreateWithAttributedString((__bridge_retained CFAttributedStringRef)attString);
    CTFrameRef theFrame = CTFramesetterCreateFrame(self.framesetter, CFRangeMake(0, [attString length]), path, NULL);

    CFRelease(path);

    CTFrameDraw(theFrame, context);
    CFRelease(theFrame);

Apparently, despite the fact that it is longer, the main text should be faster than the main graphics, and for this reason I learned how to do this. But, using this code in tools, the main graphic reaches 5 ms, and the main text - in 14 ms. I feel that something is missing here. drawInRect is almost 3 times faster, and I hear that it should be slower. I have clarified the code at the bottom as far as I know, but I am not an expert, and I would appreciate any help.

To clarify, I draw a block of text in the view. That is all I want to do. And the whole text has the same look.

+5
2

CT, , . " attString.

" ".

+2

NSMutableAttributedString   replaceCharactersInRange: withString:

, , :   [myAttString replaceCharactersInRange: NSMakeRange (0, myAttString.text.length) : newString]

. , , CGContextSetTextMatrix CTFrameDraw.

, CTFontRef CTParagraphStyleRef. " " CFRelease() 'd.

Core Text , , . , .

+1

All Articles