Oto kod używam, aby pokazać tekst w rdzeń graficzny:tekst rdzeń graficzny jest sposób szybciej niż podstawowa Tekst - czuję się jakbym czegoś brakuje
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];
A oto kod, jeśli chcę pokazać sam tekst w tekście rdzenia:
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);
Najwyraźniej, mimo że bardziej rozwlekłości, tekst rdzeń ma być szybciej niż rdzeń graficzny, który jest powodem dowiedziałem się, jak to zrobić. Ale dzięki uruchomieniu tego kodu w instrumentach, główna grafika osiąga 5 ms, podczas gdy tekst podstawowy robi to za 14 ms. Czuję, że tu coś pomijam. drawInRect jest prawie 3 razy szybszy i słyszę, że powinien być wolniejszy. Udoskonaliłem kod na dole tak bardzo jak umiem, ale nie jestem ekspertem i doceniam każdą pomoc.
Aby wyjaśnić, rysuję blok tekstu w widoku. To wszystko, co chcę zrobić. A cały tekst ma taki sam wygląd.
Siła Core Text jest w układzie tekstu, a podstawowy kod rysunkowy jest zaimplementowany w Core Graphics, nie widzę jak może być szybszy w prostym rysowaniu tekstu. – overboming