iOS CoreText 写入 PDF 和自动换行
iOS CoreText write to PDF and word wrapping
我正在对 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);
}
效果很好,但是当文本大于框架的宽度时,我无法换行。我只是在 'one line' 文本的末尾得到省略号 (...)。
这会在呈现到屏幕时换行,所以我想知道我是否遗漏了一些用于写入 pdf 并将其标记为换行的内容。谁能提供建议?
经过一些研究,LineBreak 模式似乎设置为 TruncateTail。由于此处引用的文章使用 .xib 文件来指定文本等,因此我将属性标签文本设置为 Word Wrap 但仅在该标签的属性文本检查器中。据我所见,这被忽略了,我不得不将 Line Breaks 设置设置为 Word Wrap 以获得属性更改。
我正在对 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);
}
效果很好,但是当文本大于框架的宽度时,我无法换行。我只是在 'one line' 文本的末尾得到省略号 (...)。
这会在呈现到屏幕时换行,所以我想知道我是否遗漏了一些用于写入 pdf 并将其标记为换行的内容。谁能提供建议?
经过一些研究,LineBreak 模式似乎设置为 TruncateTail。由于此处引用的文章使用 .xib 文件来指定文本等,因此我将属性标签文本设置为 Word Wrap 但仅在该标签的属性文本检查器中。据我所见,这被忽略了,我不得不将 Line Breaks 设置设置为 Word Wrap 以获得属性更改。