8

私は現在、iOS 6.1 SDK に対してアプリケーションを作成しています。iOS 7 には、私の質問に対する解決策が不要になるものがあることはわかっていますが、学習のために、とにかく質問します。

アプリは、テーブル ビューとカスタム テーブル ビューのセルで構成されます。セルの contentView の唯一のサブビューを、Core Text を使用して描画された NSAttributedString を持つカスタム ビューにしたいと考えています。各セルの文字列は異なるため、グリフの配置はグリフの数に依存する必要があります (つまり、文字列が長いほど、グリフ間の表示スペースが少なくなります)。フォントのサイズと物理的な境界は同じままである必要があり、異なるのはグリフの位置だけです。

何らかの理由で期待どおりに動作しない次のコードがあります。

BMPTeamNameView の .h は次のとおりです - カスタム ビュー (contentView のサブビュー)

@interface BMPTeamNameView : UIView

-(id)initWithFrame:(CGRect)frame text:(NSString *)text textInset:(UIEdgeInsets)insets font:(UIFont *)font;

@property (nonatomic, copy) NSAttributedString *attributedString;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, strong) UIFont *font;
@property (nonatomic, assign) UIEdgeInsets insets;

@end

新しい指定イニシャライザは、フレーム、属性付き文字列に使用するテキスト、contentView rect に対するテキスト rect を決定するインセット、および使用するフォントを設定します。

もともと私のカスタム drawRect で: CTFramesetterRef を使用しましたが、CTFramesetterRef は、個々のグリフのレイアウトを制限する (持っている可能性がありますか?) 不変のフレームを作成します。この実装では、CTTypesetterRef を使用して CTLineRef を作成します。CTFrameDraw() と CTFrameDraw() を比較すると、CTFrame を使用すると描画動作が異なりますが、それは別の質問です。私の drawRect: は次のとおりです。

- (void)drawRect:(CGRect)rect
{        
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Flips the coordinates so that drawing will be right side up
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);        
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Path that will hold the textRect
    CGMutablePathRef path = CGPathCreateMutable();

    // rectForTextInsets: returns a rect based on the insets with respect to cell contentView
    self.textRect = [self rectForTextWithInsets:self.insets];

    // Path adding / sets color for drawing
    CGPathAddRect(path, NULL, self.textRect);

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);

    CGContextAddPath(context, path);

    CGContextFillPath(context);

    // convenience method to return dictionary of attributes for string
    NSDictionary *attributes = [self attributesForAttributedString];

    // convenience method returns "Hello World" with attributes
    // typesetter property is set in the custom setAttributedString:
    self.attributedString = [self attributedStringWithAttributes:attributes];

    CTTypesetterRef typesetter = self.typesetter;

    // Creates the line for the attributed string
    CTLineRef line = CTTypesetterCreateLine(typesetter, CFRangeMake(0, 0));

    CGPoint *positions = NULL;
    CGGlyph *glyphs = NULL;
    CGPoint *positionsBuffer = NULL;
    CGGlyph *glyphsBuffer = NULL;

    // We will only have one glyph run 
    CFArrayRef glyphRuns = CTLineGetGlyphRuns(line);
    CTRunRef glyphRun = CFArrayGetValueAtIndex(glyphRuns, 0);

    // Get the count of all the glyphs
    NSUInteger glyphCount = CTRunGetGlyphCount(glyphRun);

    // This function gets the ptr to the glyphs, may return NULL
    glyphs = (CGGlyph *)CTRunGetGlyphsPtr(glyphRun);

    if (glyphs == NULL) {

        // if glyphs is NULL allocate a buffer for them
        // store them in the buffer
        // set the glyphs ptr to the buffer
        size_t glyphsBufferSize = sizeof(CGGlyph) * glyphCount;

        CGGlyph *glyphsBuffer = malloc(glyphsBufferSize);

        CTRunGetGlyphs(glyphRun, CFRangeMake(0, 0), glyphsBuffer);

        glyphs = glyphsBuffer;

    }

    // This function gets the ptr to the positions, may return NULL
    positions = (CGPoint *)CTRunGetPositionsPtr(glyphRun);

    if (positions == NULL) {

        // if positions is NULL allocate a buffer for them
        // store them in the buffer
        // set the positions ptr to the buffer
        size_t positionsBufferSize = sizeof(CGPoint) * glyphCount;

        CGPoint *positionsBuffer = malloc(positionsBufferSize);

        CTRunGetPositions(glyphRun, CFRangeMake(0, 0), positionsBuffer);

        positions = positionsBuffer;
    }

    // Changes each x by 15 and then sets new value at array index
    for (int i = 0; i < glyphCount; i++) {

        NSLog(@"positionAtIndex: %@", NSStringFromCGPoint(positions[i]));
        CGPoint oldPosition = positions[i];
        CGPoint newPosition = CGPointZero;

        NSLog(@"oldPosition = %@", NSStringFromCGPoint(oldPosition));

        newPosition.x = oldPosition.x + 15.0f;
        newPosition.y = oldPosition.y;

        NSLog(@"newPosition = %@", NSStringFromCGPoint(newPosition));

        positions[i] = newPosition;

        NSLog(@"positionAtIndex: %@", NSStringFromCGPoint(positions[i]));
    }

    // When CTLineDraw is commented out this will not display the glyphs on the screen
    CGContextShowGlyphsAtPositions(context, glyphs, positions, glyphCount);

    // When CGContextShowGlyphsAtPositions is commented out...
    // This will draw the string however it aligns the text to the view's lower left corner
    // CTFrameDraw would draw the text properly in the view's upper left corner
    // This is the difference I was speaking of and I'm not sure why it is
    CTLineDraw(line, context);


    // Make sure to release any CF objects and release allocated buffers
    CFRelease(path);
    free(positionsBuffer);
    free(glyphsBuffer);
}

なぜ CGContextShowGlyphsAtPositions() がグリフを正しく表示しないのか、なぜ CTLineDraw() が新しいグリフ位置を利用しないのか正確にはわかりません。これらの位置とグリフの割り当てを正しく処理していませんか? Caveman のデバッグは、グリフが期待どおりであり、位置が変更されていることを示しています。コードが探していたものを正確に満たしていないことはわかっています (文字列に基づいてではなく、グリフの位置を 15.0f ずつ変更していました)。

4

2 に答える 2

3

CTLineDrawからのフォントと色の情報が使用されますCFAttributedString

CGContextShowGlyphsAtPositions一方、これらを に設定する必要がありますCGContext

CGFontRef cgFont = CTFontCopyGraphicsFont(font, NULL);
CGContextSetFont(context, cgFont);
CGContextSetFontSize(context, CTFontGetSize(font));
CGContextSetFillColorWithColor(context, fillColor);

CGContextShowGlyphsAtPositions(context, glyphs, positions, glyphCount);

CFRelease(cgFont)
于 2016-02-05T14:04:54.143 に答える
3

まず、文字間隔を詰めたり緩めたりしたいだけの場合は、Core Text は必要ありません。NSKernAttributeName調整したい属性付き文字列のセクションに添付するだけです。緩めるにはプラス、締めるにはマイナス。(ゼロは「カーニングなし」を意味し、「デフォルトのカーニング」とは異なります。デフォルトのカーニングを取得するには、この属性を設定しないでください。)必要なサイズになるまでsize、onを使用しNSAttributedStringてさまざまな間隔を試すことができます。

「ポジション」は思い通りにはいきません。位置は画面座標ではありません。それらは、現在の行の左下隅である現在のテキストの原点に相対的です。(CoreText の座標は UIKit の座標とは逆になっていることに注意してください。) を呼び出すCGContextSetTextPosition()前に を呼び出す必要がありますCGContextShowGlyphsAtPositions()CTLineDraw()のラッパーCGContextShowGlyphsAtPositions()です。そのためCTLineDraw()、左下 (0,0) に描画されます。CTFrameDrawテキストの原点を正しく調整するため、左上に描画されます。

直接呼び出すとCGContextShowGlyphsAtPositions()、何も描画されないように見えます。カトランの答えはこれに対処しています。描画する前に、コンテキストのフォントと色を設定する必要があります。

あなたの再配置コードは、実際には何も役に立ちません。すべてのテキストを 15 ポイント右に移動しますが、テキスト間の間隔は実際には変更されません。(それで十分な場合は、文字列を 15 ポイント右に描画するだけです。)

現在のコードでメモリ リークが発生しています。positionsBufferとを割り当てますglyphsBufferが、これらは以前に宣言されたバージョンを隠します。だからあなたはいつもfree(NULL)最後に電話しています。

この種のテキスト調整を手動で行う完全な例については、PinchTextLayerを参照してください。ただし、属性付き文字列のカーニングを調整することで、ほぼ確実に解決できます。

于 2016-02-05T15:51:07.680 に答える