1
我正在写一个类来生成PDF,我会发布,因为我会完成它!CoreText和右对齐
我无法通过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;
}
}