【问题标题】:CGContextShowTextAtPoint and euro symbolCGContextShowTextAtPoint 和欧元符号
【发布时间】:2012-05-14 11:56:01
【问题描述】:

我在 iOS 中使用以下方法在 PDF 上绘制文本:

CGContextSelectFont (pdfContext, "Helvetica Bold", 14, kCGEncodingMacRoman);
NSString *str1 = [NSString stringWithString:@"€ 10"];  
CGContextShowTextAtPoint(pdfContext, 25, y-60 , [str1 cStringUsingEncoding:NSMacOSRomanStringEncoding], [str1 length]); 

欧元符号显示为正方形。

你有什么建议吗?

ps。我也尝试过 [str1 UTF8String],结果相同

【问题讨论】:

    标签: ios encoding pdf-generation


    【解决方案1】:

    我不知道它是否有任何作用,但是您是否尝试过使用kCGEncodingFontSpecific 作为CGContextSelectFont 的第四个参数?如果还是不行,您可能必须使用Core Text。我从未使用过 Core Text,但看起来它的文本生成比普通的 Quartz 2D 更强大。

    根据@jonathan-cichon 的建议,我还从我刚刚放置的UIViews 生成了PDF(他提出了NSString 的drawAtPoint 等,我实际上使用了普通的旧UILabels!)然后渲染对我的pdfContext来说,但这也有明显的缺点。如果您已经走上了 PDF 上下文的道路,如果 kCGEncodingFontSpecific 不这样做,我建议您追求 Core Text。有关该主题的其他想法,请参阅此SO discussion

    最后,我发现 PDF 生成有点有限(但另一方面,iOS 可以创建 PDF 真是太神奇了)。就我而言,我对 PDF 中缺少字体嵌入感到沮丧,因此我最终创建了由不太常见的字体呈现的文本图像(因为我得到了您在问题中引用的方形块)。

    【讨论】:

    • kCGEncodingFontSpecific 没有帮助,我在发布之前尝试过
    • 那么我想Core Text是要走的路。
    【解决方案2】:

    在大多数情况下,使用 NSString 方法 - (CGSize)drawAtPoint:(CGPoint)point withFont:(UIFont *)font- (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font 而不是 CoreGraphic 等价物更容易。 Apple 会为您考虑正确的转换、编码等问题。

    【讨论】:

    • 如果我用这个切换我发布的代码的最后一行:[str1 drawInRect:CGRectMake(25, y-60, 500, rowHeight) withFont:[UIFont fontWithName:@"Helvetica" size:14.0]]; 什么都没有显示
    • 在调用 drawInRect 之前是否设置了不同的填充颜色?如果它仍然是您的上下文的背景颜色,您将什么也看不到。
    • 你的意思是这样的:CGContextSetFillColorWithColor(pdfContext, [UIColor blueColor].CGColor); [str1 drawInRect:CGRectMake(25, y-60, 500, rowHeight) withFont:[UIFont fontWithName:@"Helvetica" size:14.0]]; 在这种情况下,我什么也看不到,我得到 : CGContextSetFont: invalid context 0x0
    • 似乎您没有当前上下文。如果您不使用 UIGraphicsBeginPDFContext... 创建 pdf 上下文,则必须使用 UIGraphicsPushContext(context) 以使您的上下文可通过大多数 UIKit 方法所依赖的 UIGraphicsGetCurrentContext() 访问。
    • 我试过了,我现在可以看到欧元符号,但文字是旋转的(180 度)。 UIGraphicsPushContext(pdfContext); CGContextSetFillColorWithColor(pdfContext, [UIColor blackColor].CGColor); CGRect rect = CGRectMake(25, y-60, 100, rowHeight); UIFont *font = [UIFont fontWithName:@"Helvetica" size:14.0]; [str1 drawInRect:rect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; UIGraphicsPopContext();如何将文字旋转180度
    猜你喜欢
    • 1970-01-01
    • 2010-11-26
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多