【问题标题】:CoreText and right alignmentCoreText 和右对齐
【发布时间】:2012-10-29 02:32:13
【问题描述】:

我正在编写一个生成 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;
}

}

【问题讨论】:

    标签: ios core-text


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 2018-01-24
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      相关资源
      最近更新 更多