我目前正在编写一个针对 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
新的指定初始化器现在将设置框架、用于属性字符串的文本、确定文本 rect 相对于 contentView rect 的 insets 以及要使用的字体。
最初在我的自定义 drawRect 中:我使用了 CTFramesetterRef,但是 CTFramesetterRef 将创建一个不可变的框架,它(可能?)限制了单个字形的布局。在这个实现中,我使用 CTTypesetterRef 来创建 CTLineRef。当您比较 CTLineDraw() 和 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() 不会使用新的字形位置。我是否错误地处理了这些位置和字形的分配?穴居人调试显示字形与预期一致,并且位置正在更改。我知道我的代码并不能完全满足我正在寻找的内容(我将字形位置更改为 15.0f 而不是基于字符串)但是,我在布置这些字形时哪里出错了?