ios - CTRunGetImageBounds 返回不准确的结果

标签 ios core-text

我正在使用 Core Text 绘制一些文本。我想获得各种运行边界,但是当我调用 CTRunGetImageBounds 时,返回的矩形大小正确,但位置错误。具体来说,行原点在整个文本的末尾。

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    self.transform = CGAffineTransformMakeScale(1.0, -1.0);
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);

    [[UIColor whiteColor] set];
    CGContextFillRect(context, self.bounds);

    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"Blue should be underlined."];
    NSRange blueRange = NSMakeRange(0, 4);
    [attrString beginEditing];
    //make all text size 20.0
    [attrString addAttribute:(NSString *)kCTFontAttributeName value:(id)CTFontCreateWithName((CFStringRef)@"Helvetica", 20.0, NULL) range:NSMakeRange(0, [attrString length])];
    //make the text appear in blue
    [attrString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:blueRange];
    //next make the text appear with an underline
    [attrString addAttribute:(NSString *)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:blueRange];
    [attrString endEditing];

    CGMutablePathRef path = CGPathCreateMutable();
    CGRect bounds = CGRectMake(10.0, 10.0, 200.0, 200.0);
    [[UIColor redColor] set];
    CGContextFillRect(context, bounds);
    CGPathAddRect(path, NULL, bounds);

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
    CTFrameDraw(frame, context);

    for (id lineObj in (NSArray *)CTFrameGetLines(frame)) {
        CTLineRef line = (CTLineRef)lineObj;
        for (id runObj in (NSArray *)CTLineGetGlyphRuns(line)) {
            CTRunRef run = (CTRunRef)runObj;
            CGRect runBounds = CTRunGetImageBounds(run, context, CFRangeMake(0, 0));
            NSLog(@"bounds: %@", NSStringFromCGRect(runBounds));

            [[UIColor greenColor] set];
            CGContextFillRect(context, runBounds);
        }
    }

    CFRelease(framesetter);
    CFRelease(frame);
    [attrString release];
}

产生:

results

最佳答案

这是我想出的。这是完全准确的。

CTFrameRef frame = [self _frameWithRect:rect];

NSArray *lines = (NSArray *)CTFrameGetLines(frame);

CGPoint origins[[lines count]];//the origins of each line at the baseline
CTFrameGetLineOrigins(frame, CFRangeMake(0, 0), origins);

NSUInteger lineIndex = 0;
for (id lineObj in lines) {
    CTLineRef line = (CTLineRef)lineObj;

    for (id runObj in (NSArray *)CTLineGetGlyphRuns(line)) {
        CTRunRef run = (CTRunRef)runObj;
        CFRange runRange = CTRunGetStringRange(run);

        CGRect runBounds;

        CGFloat ascent;//height above the baseline
        CGFloat descent;//height below the baseline
        runBounds.size.width = CTRunGetTypographicBounds(run, CFRangeMake(0, 0), &ascent, &descent, NULL);
        runBounds.size.height = ascent + descent;

        CGFloat xOffset = CTLineGetOffsetForStringIndex(line, CTRunGetStringRange(run).location, NULL);
        runBounds.origin.x = origins[lineIndex].x + rect.origin.x + xOffset;
        runBounds.origin.y = origins[lineIndex].y + rect.origin.y;
        runBounds.origin.y -= descent;

        //do something with runBounds
    }
    lineIndex++;
}

关于ios - CTRunGetImageBounds 返回不准确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5213186/

相关文章:

ios - 无法在 Swift 中将一个结构分配给另一个结构

objective-c - iOS 图像处理(失真)

ios - 属性类型 'id' 与继承自 'id' 警告的类型 'UIToolbar' 不兼容

ios - 可滚动折线图

windows-phone-7 - Window Phone 是否有相当于 iOS Core Text 的工具

algorithm - 在不规则形状内渲染 CoreText

iphone - 需要 iPhone 屏幕录制

iPhone 核心文本 : Find the pixel coordinates of a substring

objective-c - CoreText 框架 - 应用程序无法在模拟器中运行 - iOS

iOS - 如何在 PDF 文档顶部添加文本?