2012-10-26 5 views
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; 
} 

}

답변

2

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);