CTFrameGetLineOrigins中的错误?
  我使用Apple的SimpleTextInput示例中的以下代码来确定游标的框架。  我正在使用CTFrameGetLineOrigins并且我注意到它返回的y值是不正确的(它显示.336,无论哪一行都在)。  我的应用程序与SimpleTextInput有点不同,因为我垂直居中我的文本,因此文本框架不是直接从原点开始。  但是,为什么它给了我这个永远的价值?  这是一个错误? 
for (int i = 0; i < [lines count]; i++) 
{
    CTLineRef line = (CTLineRef) [lines objectAtIndex:i];
    CFRange range = CTLineGetStringRange(line);
    NSInteger localIndex = index - range.location;
    if (localIndex >= 0 && localIndex <= range.length) 
    {
        CGPoint origin;
        CGFloat ascent, descent;
        CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
        CTFrameGetLineOrigins(self.textFrame, CFRangeMake(i, 0), &origin);
        CGFloat xPos = origin.x + CTLineGetOffsetForStringIndex(line, index, NULL);
        return CGRectMake(xPos, origin.y - descent, 3, ascent + descent);
    }
}
- (void) drawRect:(CGRect)rect
{
    CTFramesetterRef textFramesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.text);
    CGMutablePathRef textFrameMutablePath = CGPathCreateMutable();
    CGPathAddRect(textFrameMutablePath, NULL, self.bounds);
    self.textFrame = CTFramesetterCreateFrame(textFramesetter, CFRangeMake(0, 0), textFrameMutablePath, NULL);
    CGFloat textFrameHeight = [self calculateHeightForTextFrame];
    CGRect textFrameRect = CGRectMake(0, (self.bounds.size.height / 2) - (textFrameHeight / 2), self.bounds.size.width, textFrameHeight);
    CGPathRelease(textFrameMutablePath);
    textFrameMutablePath = NULL;
    CFRelease(self.textFrame);
    self.textFrame = NULL;
    textFrameMutablePath = CGPathCreateMutable();
    CGPathAddRect(textFrameMutablePath, NULL, textFrameRect);
    self.textFrame = CTFramesetterCreateFrame(textFramesetter, CFRangeMake(0, 0), textFrameMutablePath, NULL);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CTFrameDraw(self.textFrame, context); 
    CGPathRelease(textFrameMutablePath);
    CFRelease(textFramesetter);
    [self cursorPositionChanged];
}
Take out CTFrameGetLineOrigins() out of loop to get all line origins like :
NSArray *lines = (__bridge id)CTFrameGetLines(frame);
CGPoint origins[lines.count];
CTFrameGetLineOrigins(frame, CFRangeMake(0, lines.count), origins); //this will fill all line origins in origins[] buffer.
for (int i = 0; i < [lines count]; i++)
{
    CTLineRef line = (__bridge CTLineRef) [lines objectAtIndex:i];
    CFRange range = CTLineGetStringRange(line);
    // CGPoint origin; // moved out of loop
    CGFloat ascent, descent;
    CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
    // CTFrameGetLineOrigins(frame, CFRangeMake(i, 1), &origin); // moved to out of loop
    CGFloat xPos = origins[i].x; //  + CTLineGetOffsetForStringIndex(line, index, NULL);
    NSLog(@"%@", NSStringFromCGRect(CGRectMake(xPos + origins[i].x, origins[i].y - descent, 3, ascent + descent)));
}
你的范围是0元素长:
CFRangeMake(i, 0)
你打算让它长1个元素:
CFRangeMake(i, 1)
编辑:
我只是尝试了下面的代码,并没有问题为每一行获得不同的起源。 我拿出你的“索引”代码,因为我不知道应该设置什么索引,而且我想证明我可以获得所有行的起源(我可以)。
CTFramesetterRef
      framesetter = CTFramesetterCreateWithAttributedString(attrString);
  CFArrayRef paths = [self copyPaths];
  CFIndex pathCount = CFArrayGetCount(paths);
  CFIndex charIndex = 0;
  for (CFIndex pathIndex = 0; pathIndex < pathCount; ++pathIndex) {
    CGPathRef path = CFArrayGetValueAtIndex(paths, pathIndex);
    CTFrameRef
        frame = CTFramesetterCreateFrame(framesetter,
                                         CFRangeMake(charIndex, 0),
                                         path,
        NULL);
    NSArray *lines = (__bridge id)CTFrameGetLines(frame);
    for (int i = 0; i < [lines count]; i++)
    {
      CTLineRef line = (__bridge CTLineRef) [lines objectAtIndex:i];
      CFRange range = CTLineGetStringRange(line);
      CGPoint origin;
      CGFloat ascent, descent;
      CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
      CTFrameGetLineOrigins(frame, CFRangeMake(i, 1), &origin);
      CGFloat xPos = origin.x; //  + CTLineGetOffsetForStringIndex(line, index, NULL);
      NSLog(@"%@", NSStringFromCGRect(CGRectMake(xPos, origin.y - descent, 3, ascent + descent)));
    }
它在我的情况下输出:
2012-03-21 18:28:19.834 Columns[9370:f803] {{0, 929.24}, {3, 12}}
2012-03-21 18:28:19.835 Columns[9370:f803] {{0, 914.24}, {3, 12}}
2012-03-21 18:28:19.835 Columns[9370:f803] {{0, 899.24}, {3, 12}}
2012-03-21 18:28:19.836 Columns[9370:f803] {{0, 884.24}, {3, 12}}
2012-03-21 18:28:19.836 Columns[9370:f803] {{0, 869.24}, {3, 12}}
2012-03-21 18:28:19.836 Columns[9370:f803] {{0, 854.24}, {3, 12}}
2012-03-21 18:28:19.837 Columns[9370:f803] {{0, 839.24}, {3, 12}}
2012-03-21 18:28:19.837 Columns[9370:f803] {{0, 824.24}, {3, 12}}
2012-03-21 18:28:19.838 Columns[9370:f803] {{0, 809.24}, {3, 12}}
2012-03-21 18:28:19.838 Columns[9370:f803] {{0, 794.24}, {3, 12}}
2012-03-21 18:28:19.838 Columns[9370:f803] {{0, 779.24}, {3, 12}}
2012-03-21 18:28:19.839 Columns[9370:f803] {{0, 764.24}, {3, 12}}
...
奇怪的是,我似乎已经解决了这个问题,取而代之:
CGPathAddRect(textFrameMutablePath, NULL, self.bounds);
同
CGPathAddRect(textFrameMutablePath, NULL, CGRectInset(self.bounds, self.horizontalInset, self.verticalInset));
不知道为什么,但它的工作原理。
链接地址: http://www.djcxy.com/p/81939.html上一篇: Bug in CTFrameGetLineOrigins?
下一篇: How to highlight CoreText with changing highlight colors?
