我正在为我的应用程序开发文本添加功能。该文本将使用 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];
我遇到了一些问题:
-
文本渲染翻转 - (这可以通过转换轻松纠正,但我遵循的教程没有这个问题,所以我有点困惑LINK)。
-
文本呈现为奇怪的字符 - 我认为这是一个编码问题,但我该如何调整?
-
文本不会添加到 addSubview
行的视图中,只有在更新 CGContext 时才会添加文本,即我也有一个画线功能,并且如果我在视图上绘制然后文本神奇地弹出。
-
文本锯齿严重且渲染得很糟糕,我可以为此做些什么吗?我添加了一个 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:
-
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).
-
The text is rendering with strange characters - I think this is an encoding issue, but how do i adjust this?
-
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.
-
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:

Thanks for any help you can give.
发布评论
评论(2)
对于文本翻转,发布了针对具有相同问题的人的解决方案 文本倒置问题解决方案[Stackoverflow] - 使用:
对于编码问题,您得到带有
[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:
For the encoding problem, you are getting the text cString with
[text UTF8String]
(UTF8 characters are in general multibyte) butCGContextSelectFont()
is specifying MacRoman (a single byte format).Julian
我已经找出了大部分错误。
使用基本转换,我能够自动翻转文本。
这里我调整了编码,因此始终使用 MacRoman。
我必须手动调用更新 - 我编写了一个函数来执行此操作并在需要的地方调用它。
由于文本打印陷入循环,它似乎将文本多次打印在彼此之上,因此出现锯齿状。
I've figured out the bulk of these errors.
Using a basic transformation, I was able to flip the text automatically.
Here I adjusted the encoding so MacRoman was used throughout.
I had to manually invoke an update - I wrote a function that does just that and called it where I needed it.
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.