Iphone CTFrameGetLineOriginates获取了不正确的来源

Iphone CTFrameGetLineOriginates获取了不正确的来源,iphone,ios,core-text,Iphone,Ios,Core Text,我使用CTFrameGetLineOriginates获取每行的原点。但它总是返回错误的行原点 守则: CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextTranslateCTM(context, 0, self.bounds.size.height); CGContextScaleCTM(contex

我使用CTFrameGetLineOriginates获取每行的原点。但它总是返回错误的行原点

守则:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

CTFontRef font = CTFontCreateUIFontForLanguage(kCTFontUserFontType, self.font.pointSize, NULL);

CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTForegroundColorAttributeName, self.textColor.CGColor);
CFAttributedStringSetAttribute(attrString, CFRangeMake(0, CFAttributedStringGetLength(attrString)), kCTFontAttributeName, font);

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CGPathRef path = CGPathCreateWithRect(CGRectInset(rect, TEXT_MARGIN, TEXT_MARGIN), NULL);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

CFArrayRef lines = CTFrameGetLines(frame);
size_t numOfLines = CFArrayGetCount(lines);
CGPoint lineOrigins[numOfLines];
CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), lineOrigins);
for (CFIndex i = 0; i < numOfLines; i++) {
    CTLineRef line = CFArrayGetValueAtIndex(lines, i);
    CFArrayRef runs = CTLineGetGlyphRuns(line);
    size_t numOfRuns = CFArrayGetCount(runs);

    for (CFIndex j = 0; j < numOfRuns; j++) {
        CTRunRef run = CFArrayGetValueAtIndex(runs, j);

        CGRect strFrame = CGRectZero;
        CGFloat width = 0;
        CGFloat height = 0;
        CGFloat leading = 0;
        CGFloat ascent = 0;
        CGFloat descent = 0;
        CFRange strRange = CTRunGetStringRange(run);
        CGFloat offsetX = CTLineGetOffsetForStringIndex(line, strRange.location, NULL);

        width = (CGFloat)CTRunGetTypographicBounds(run, CFRangeMake(0, 0), &ascent, &descent, &leading);
        width += leading;
        height = ascent + descent;
        NSLog(@"Line %ld : Offset Y: %f", i+1, lineOrigins[i].y);
        strFrame = CGRectMake(lineOrigins[i].x + offsetX + TEXT_MARGIN, lineOrigins[i].y + TEXT_MARGIN - descent, width, height);
        strFrame = CGRectIntegral(strFrame);
        CFDictionaryRef attr = CTRunGetAttributes(run);
        if (attr != NULL) {
            CFStringRef url = CFDictionaryGetValue(attr, kCTFontURLAttribute);
            if (url != NULL) {
                [self.wordsFrame addObject:[NSDictionary dictionaryWithObjectsAndKeys:(__bridge NSString *)url, @"URL", NSStringFromCGRect(strFrame), @"frame", nil]];
            }
        }
    }
}
CTFrameDraw(frame, context);

CFRelease(font);
CGPathRelease(path);
CFRelease(frame);
CFRelease(framesetter);
有人能帮我找出我的代码出了什么问题吗?或者是否有其他方法获得每个CTRun的原点?

我得到了:
strFrame=CGRectMake(lineOrigins[i].x+offsetX+TEXT\u MARGIN,self.bounds.size.height-(lineOrigins[i].y+TEXT\u MARGIN-descent)-高度、宽度、高度)

需要翻转y坐标吗

2012-05-30 12:58:21.464 hjday[9362:707] Line 1 : Offset Y: 97.000000
2012-05-30 12:58:21.466 hjday[9362:707] Line 2 : Offset Y: 68.000000
2012-05-30 12:58:21.472 hjday[9362:707] Line 3 : Offset Y: 39.000000