私は現在、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 ずつ変更していました)。