Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 核心文本和右对齐_Ios_Core Text - Fatal编程技术网

Ios 核心文本和右对齐

Ios 核心文本和右对齐,ios,core-text,Ios,Core Text,我正在编写一个类来生成PDF,我将在完成后发布它 我无法将文本向右对齐,使用CTParagraphStyle,文本始终位于左侧。这怎么可能呢?我做错了什么 - (void)addText:(NSString *)text color:(UIColor *)color fontSize:(CGFloat)size floating:(BOOL)floating { CGContextSaveGState(pdfContext); // Prepare font CTFontRef font =

我正在编写一个类来生成PDF,我将在完成后发布它

我无法将文本向右对齐,使用CTParagraphStyle,文本始终位于左侧。这怎么可能呢?我做错了什么

- (void)addText:(NSString *)text color:(UIColor *)color fontSize:(CGFloat)size floating:(BOOL)floating {
CGContextSaveGState(pdfContext);

// Prepare font
CTFontRef font = CTFontCreateWithName(CFSTR("Verdana"), size, NULL);

// Font color
CGColorRef fontColor = [color CGColor];

// Paragraph
CTTextAlignment alignment = kCTRightTextAlignment;

CTParagraphStyleSetting settings[] = {
    {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment}
};

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));

// Create an attributed string
CFStringRef keys[] = { kCTFontAttributeName , kCTParagraphStyleAttributeName, kCTForegroundColorAttributeName};
CFTypeRef values[] = { font, paragraphStyle, fontColor};
CFDictionaryRef attr = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values,
                                          sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFAttributedStringRef attrString = CFAttributedStringCreate(NULL, (CFStringRef)text, attr);

CFRelease(paragraphStyle);
CFRelease(attr);

// Draw the string
CTLineRef line = CTLineCreateWithAttributedString(attrString);

CGContextSetTextPosition(pdfContext, xPadding, [self relativeHeight:currentHeight+size]);


CTLineDraw(line, pdfContext);

// Clean up
CFRelease(line);
CFRelease(attrString);
CFRelease(font);

CGContextRestoreGState(pdfContext);

if(floating == NO) {
    currentHeight += size;
}
}

删除
CTLineDraw()
及其相关代码,然后使用
CTFrameDraw()

试试这个:

// Create the Core Text framesetter using the attributed string.
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);

// Create the Core Text frame using our current view rect bounds.
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
CTFrameRef frame =  CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), [path CGPath], NULL);
CTFrameDraw(frame, pdfContext);