5

コアグラフィックスにテキストを表示するために使用するコードは次のとおりです。

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];

そして、コアテキストに同じテキストを表示したい場合のコードは次のとおりです。

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);

どうやら、より長文ではあるものの、コア テキストはコア グラフィックよりも高速であるように意図されているようです。これが、私がその方法を学んだ理由です。しかし、インストゥルメントでこのコードを実行すると、コア グラフィックスは 5 ミリ秒を達成し、コア テキストは 14 ミリ秒で実行されます。ここで何かが欠けているように感じます。drawInRect はほぼ 3 倍高速ですが、遅くする必要があると聞きました。一番下のコードをできる限り改良しましたが、私は専門家ではないので、助けていただければ幸いです。

明確にするために、ビューにテキストのブロックを描画しています。それが私のやりたいことのすべてです。そして、すべてのテキストの外観は同じです。

4

2 に答える 2

2

私はCTのものを使用していませんが、描画ごとに不変のフォントやその他の属性を設定しているように見えます. コメント「座標系を反転し、attString をキャッシュする」までのすべてを因数分解してみてください。

これにより、より良い直接対決テストの速度が向上するはずです。

于 2012-05-16T03:37:43.097 に答える
1

NSMutableAttributedString では、replaceCharactersInRange:withString を使用して文字列を変更できます。

属性ディクショナリを設定したら、[myAttString replaceCharactersInRange:NSMakeRange(0, myAttString.text.length) withString:newString] を使用して、属性付き文字列への以降の文字列更新を制限できます。

汚れた四角形を描きます。また、CTFrameDraw を使用する場合、CGContextSetTextMatrix はほとんど意味がないことがわかりました。

もう 1 つ簡単なメモとして、CTFontRef と CTParagraphStyleRef に関するリリースはありません。また、「作成ルール」に従い、CFRelease() する必要があります。

私は最近、コアテキストの学習曲線に苦労しており、いくつかの有望な結果を達成しているようです. あなた自身の最適化があなたに同様の希望を与えることを願っています.

于 2013-06-26T02:29:43.940 に答える