0

所有x坐标都是正确的,但所有y坐标都不正确 - 总是3.40282e+38。如何为每个CTLine获得正确的y

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attString);

CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, self.frameSize.width, CGFLOAT_MAX));
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, self.attString.length), path, NULL);
CFArrayRef lines = CTFrameGetLines(frame);
size_t numOfLines = CFArrayGetCount(lines);
CGPoint lineOrigins[numOfLines];
CTFrameGetLineOrigins(frame, CFRangeMake(0, numOfLines), lineOrigins);

NSLog(@"Lines count %ld", numOfLines);
for (CFIndex i = 0; i < numOfLines; i++)
{
    CTLineRef line = CFArrayGetValueAtIndex(lines, i);
    [self draw:frame line:line];

    NSLog(@"%@", NSStringFromCGPoint(lineOrigins[i]));
}

输出:

2014-04-11 10:45:27.095 Backgammon HD Narde[2221:707] {0, 3.40282e+38}
2014-04-11 10:45:27.099 Backgammon HD Narde[2221:707] {0, 3.40282e+38}
2014-04-11 10:45:27.105 Backgammon HD Narde[2221:707] {84.997, 3.40282e+38}
2014-04-11 10:45:27.111 Backgammon HD Narde[2221:707] {0, 3.40282e+38}
2014-04-11 10:45:27.117 Backgammon HD Narde[2221:707] {0, 3.40282e+38}
2014-04-11 10:45:27.121 Backgammon HD Narde[2221:707] {0, 3.40282e+38}
2014-04-11 10:45:27.124 Backgammon HD Narde[2221:707] {0, 3.40282e+38}
2014-04-11 10:45:27.126 Backgammon HD Narde[2221:707] {0, 3.40282e+38}
2014-04-11 10:45:27.128 Backgammon HD Narde[2221:707] {136.804, 3.40282e+38}
2014-04-11 10:45:27.132 Backgammon HD Narde[2221:707] {0, 3.40282e+38}
4

2 回答 2

1

你得到正确的坐标,但 CoreText 原点是“左下角”,当你使用一个巨大的高度时,CGFLOAT_MAX你看不到它们。使用较小高度 (1e7) 并显示基准线和翻转线坐标的示例代码(基于您的代码)。

 NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet, ..."];

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString);

CGMutablePathRef path = CGPathCreateMutable();
// used a smaller height
CGPathAddRect(path, NULL, CGRectMake(0, 0, self.view.frame.size.width, 1e7));
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);

// transform to flip coordinate
CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 1e7);
transform = CGAffineTransformScale(transform, 1, -1);

NSLog(@"Lines count %ld", numOfLines);
for (CFIndex i = 0; i < numOfLines; i++)
{
    CGPoint flipped; // flipped line location
    flipped = CGPointApplyAffineTransform(lineOrigins[i], transform);

    NSLog(@"origin=%@ flipped=%@", NSStringFromCGPoint(lineOrigins[i]), NSStringFromCGPoint(flipped));
}

日志输出:

Lines count 8
origin={0, 9.99999e+06} flipped={0, 12}
origin={0, 9.99997e+06} flipped={0, 27}
origin={0, 9.99996e+06} flipped={0, 42}
origin={0, 9.99994e+06} flipped={0, 57}
origin={0, 9.99993e+06} flipped={0, 72}
origin={0, 9.99991e+06} flipped={0, 87}
origin={0, 9.9999e+06} flipped={0, 102}
origin={0, 9.99988e+06} flipped={0, 117}
于 2014-04-11T11:17:22.967 回答
0

正确方法:

使用 CTFramesetterCreateWithAttributedString 获取文本框架。使用返回值的高度。

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self.attString);
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,
                                                                    CFRangeMake(0, self.attString.length),
                                                                    NULL,
                                                                    CGSizeMake(self.width, CGFLOAT_MAX),
                                                                    NULL);
suggestedSize.width = self.width;
suggestedSize.height = ceilf(suggestedSize.height);
[self setContentSize:suggestedSize];


CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, suggestedSize.width, suggestedSize.height));
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, self.attString.length), path, NULL);
CFArrayRef lines = CTFrameGetLines(frame);
size_t numOfLines = CFArrayGetCount(lines);
CGPoint lineOrigins[numOfLines];
CTFrameGetLineOrigins(frame, CFRangeMake(0, numOfLines), lineOrigins);

for (CFIndex i = 0; i < numOfLines; i++)
{
    CTLineRef line = CFArrayGetValueAtIndex(lines, i);
    CCSprite *sprite = [self draw:frame line:line];
    NSLog(@"%@", NSStringFromCGPoint(lineOrigins[i]));
    sprite.position = lineOrigins[i];
}
于 2014-04-14T14:36:25.880 回答