iOS CoreText写入PDF和 自动换行

问题描述 投票:0回答:1

我正在使用rawenderlich.com这篇优秀文章中的代码稍作修改,将一些文字写入PDF:

-(void)drawText:(NSString*)text inFrame:(CGRect)frameRect withAttributes:(NSDictionary*)attributes
{
    CFStringRef stringRef = (__bridge CFStringRef)text;

    // Prepare the text using a Core Text Framesetter
    CFDictionaryRef attributeRef = (__bridge CFDictionaryRef)attributes;

    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, attributeRef);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, frameRect);

    // Get the frame that will do the rendering.
    CFRange currentRange = CFRangeMake(0, 0);
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);

    // Get the graphics context.
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    // Put the text matrix into a known state. This ensures
    // that no old scaling factors are left in place.
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

    // Core Text draws from the bottom-left corner up, so flip
    // the current transform prior to drawing.
    CGFloat offset = (frameRect.origin.y*2)+frameRect.size.height;
    CGContextTranslateCTM(currentContext, 0, offset);
    CGContextScaleCTM(currentContext, 1.0, -1.0);

    // Draw the frame.
    CTFrameDraw(frameRef, currentContext);

    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CGContextTranslateCTM(currentContext, 0, -offset);

    CFRelease(frameRef);
    CFRelease(framesetter);
    CFRelease(currentText);
}

这很好用,但是当它大于帧的宽度时,我无法将文本换行。我只是在我的“一行”文本的末尾得到省略号(...)。

当渲染到屏幕时这会换行,所以我想知道我是否遗漏了写入pdf并将其标记为包装的内容。有人可以提供建议吗?

ios pdf render word-wrap core-text
1个回答
1
投票

经过一些研究后,似乎将LineBreak模式设置为TruncateTail。由于此处引用的文章使用.xib文件来指定文本等,我将属性标签文本设置为Word Wrap,但仅限于该标签的属性文本检查器中。从我可以看到,这被忽略,我不得不将换行设置为Word Wrap以获得属性更改。

© www.soinside.com 2019 - 2024. All rights reserved.