Quartz 2D 文本绘制在编码和转换中出现错误?

发布于 2024-11-14 05:36:42 字数 1539 浏览 3 评论 0 原文

我正在为我的应用程序开发文本添加功能。该文本将使用 CGContext 和 Quartz 2D 添加。变量 text 是从 UITextField 的 text 属性中提取的。

这是我到目前为止的代码:

CGRect textBox = CGRectMake(0, 0, 300, 200);
UIView *textView = [[UIView alloc] initWithFrame:textBox];
const char *textChar = [text UTF8String];   

NSInteger xLocation = (contentOffset.x + 100);
CGContextSelectFont (UIGraphicsGetCurrentContext(), "Helvetica", 14.0f, kCGEncodingMacRoman);
CGContextSetShouldSmoothFonts(UIGraphicsGetCurrentContext(), YES);
CGContextSetCharacterSpacing (UIGraphicsGetCurrentContext(), 10);
CGContextSetTextDrawingMode (UIGraphicsGetCurrentContext(), kCGTextFill);
CGContextSetRGBFillColor (UIGraphicsGetCurrentContext(), 255, 255, 255, 1.0); 
CGContextShowTextAtPoint (UIGraphicsGetCurrentContext(), xLocation, 110, textChar, 9);
[drawGallery addSubview:textView];

我遇到了一些问题:

  1. 文本渲染翻转 - (这可以通过转换轻松纠正,但我遵循的教程没有这个问题,所以我有点困惑LINK)。

  2. 文本呈现为奇怪的字符 - 我认为这是一个编码问题,但我该如何调整?

  3. 文本不会添加到 addSubview 行的视图中,只有在更新 CGContext 时才会添加文本,即我也有一个画线功能,并且如果我在视图上绘制然后文本神奇地弹出。

  4. 文本锯齿严重且渲染得很糟糕,我可以为此做些什么吗?我添加了一个 CGContextSetShouldSmoothFonts 但它似乎没有做任何事情。

请参阅下面的示例:

奇怪的字符

感谢您提供的任何帮助。

I'm working on a Text Addition feature for my App. This text is going to be added with CGContext and Quartz 2D. The variable text is pulled from a UITextField's text property.

Here is the code I have so far:

CGRect textBox = CGRectMake(0, 0, 300, 200);
UIView *textView = [[UIView alloc] initWithFrame:textBox];
const char *textChar = [text UTF8String];   

NSInteger xLocation = (contentOffset.x + 100);
CGContextSelectFont (UIGraphicsGetCurrentContext(), "Helvetica", 14.0f, kCGEncodingMacRoman);
CGContextSetShouldSmoothFonts(UIGraphicsGetCurrentContext(), YES);
CGContextSetCharacterSpacing (UIGraphicsGetCurrentContext(), 10);
CGContextSetTextDrawingMode (UIGraphicsGetCurrentContext(), kCGTextFill);
CGContextSetRGBFillColor (UIGraphicsGetCurrentContext(), 255, 255, 255, 1.0); 
CGContextShowTextAtPoint (UIGraphicsGetCurrentContext(), xLocation, 110, textChar, 9);
[drawGallery addSubview:textView];

I have a few problems with this:

  1. The text is rendering flipped - (this is easily rectified with a transform, but the tutorial I followed didn't have this issue so I'm confused a little LINK).

  2. The text is rendering with strange characters - I think this is an encoding issue, but how do i adjust this?

  3. The text doesn't get added to the view at the addSubview line, it only gets added when the CGContext is updated, i.e. I have a draw lines function too and if I draw on the view then the text magically pops up.

  4. The text is horribly jagged and badly rendered, is there something I can do for this? I've added a CGContextSetShouldSmoothFonts but it doesn't seem to be doing anything.

See below for an example:

strange characters

Thanks for any help you can give.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2024-11-21 05:36:42

对于文本翻转,发布了针对具有相同问题的人的解决方案 文本倒置问题解决方案[Stackoverflow] - 使用:

CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));                  

对于编码问题,您得到带有 [text UTF8String] 的文本 cString(UTF8 字符通常是多字节),但 CGContextSelectFont() 指定 MacRoman(单字节格式)。

朱利安

For the text flipping, a solution to someone with the same problem was posted text inverted problem solution [Stackoverflow] - use:

CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));                  

For the encoding problem, you are getting the text cString with [text UTF8String] (UTF8 characters are in general multibyte) but CGContextSelectFont() is specifying MacRoman (a single byte format).

Julian

流绪微梦 2024-11-21 05:36:42

我已经找出了大部分错误。

  1. 文本呈现翻转

  2. 文本正在呈现奇怪的字符 - 我认为这是一个
    编码问题,但我该如何调整
    这个?

  3. 文本不会添加到 addSubview 行的视图中,它只会添加到视图中
    当 CGContext 为
    更新了,即我有一条画线
    功能也一样,如果我在视图上绘图
    然后文字神奇地弹出。

  4. 文本锯齿严重且渲染得很糟糕,我有什么问题吗?
    可以为此做些什么吗?我添加了一个
    CGContextSetShouldSmoothFonts 但它
    似乎什么也没做。

  1. 使用基本转换,我能够自动翻转文本。

  2. 这里我调整了编码,因此始终使用 MacRoman。

  3. 我必须手动调用更新 - 我编写了一个函数来执行此操作并在需要的地方调用它。

  4. 由于文本打印陷入循环,它似乎将文本多次打印在彼此之上,因此出现锯齿状。

I've figured out the bulk of these errors.

  1. The text is rendering flipped

  2. The text is rendering with strange characters - I think this is an
    encoding issue, but how do i adjust
    this?

  3. The text doesn't get added to the view at the addSubview line, it only
    gets added when the CGContext is
    updated, i.e. I have a draw lines
    function too and if I draw on the view
    then the text magically pops up.

  4. The text is horribly jagged and badly rendered, is there something I
    can do for this? I've added a
    CGContextSetShouldSmoothFonts but it
    doesn't seem to be doing anything.

  1. Using a basic transformation, I was able to flip the text automatically.

  2. Here I adjusted the encoding so MacRoman was used throughout.

  3. I had to manually invoke an update - I wrote a function that does just that and called it where I needed it.

  4. Due to the text print being caught in a loop, it appeared to print the text several times on top of each other, hence the jaggedness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文