如何绘制旋转文本而不限于 MacRoman 字符集?
我的博客之前已经提到过,作为问题的答案。我无法链接到其中一个,因为此表单声称我是垃圾邮件发送者。叹。不管怎样,现在我想写一篇关于绘制旋转文本的权威博客文章,所以它也可以用于这里的答案(呵呵)。这是我的第一次尝试:
http:// /www.platinumball.net/blog/2009/06/01/drawing-nsstrings-in-unusual-rotations/
效果很好,它可以满足我想要的一切,除了事实上它使用 CGContextSelectFont(),它将文本输出限制为 MacRoman。那是德苏克。
我已经寻找这个问题的答案近一年了,包括多次搜索该网站,但没有成功。我见过一些看起来很接近我想要的例子,但显然我太愚蠢了,无法让它们屈服于我的意愿。我包含了我编写的 NSString 类别中的 main 方法,它显示了我正在做的事情的实质。如果您需要查看其余的代码,可以从我上面给出的博客链接下载。谢谢 ...
-(void)drawAtPoint:(CGPoint)point withFont:(UIFont*)font
orientation:(WBOrientation)orient
{
CGContextRef ctxt = [self contextSave];
const std::string tbuf = toStdString(self, NSMacOSRomanStringEncoding);
CGAffineTransform tran = CGAffineTransformMake(1, 0, 0, -1, 0, 0);
switch (orient)
{
case WBOrientUp:
// nothing to do
break;
case WBOrientDown:
tran = CGAffineTransformRotate(tran, degreesToRadians(180.0));
break;
case WBOrientLeft:
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;
case WBOrientRight:
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;
default:
assert(false);
break;
}
contextFont(ctxt, font);
CGContextSetTextDrawingMode(ctxt, kCGTextFill);
CGContextSetTextMatrix(ctxt, tran);
CGContextSetTextPosition(ctxt, point.x, point.y);
CGContextShowText(ctxt, tbuf.c_str(), tbuf.length());
[self contextRestore:ctxt];
}
My blog has been mentioned here before as an answer to questions. I can't link to one of them, because this form claims I'm a spammer. Sigh. Anyway, now I want to write the definitive blog post on drawing rotated text, so it can also be used for answers here (heh). Here is my first try:
http://www.platinumball.net/blog/2009/06/01/drawing-nsstrings-in-unusual-rotations/
That works well, it does everything I want, except for the fact that it uses CGContextSelectFont(), which limits text output to MacRoman. That is Teh Sukk.
I've been searching for an answer to this problem for almost a year now, including searching this site several times, without success. I've seen examples that seem close to what I want, but apparently I'm too stupid to bend them to my will. I'm including the main method from the NSString category I wrote, which shows the guts of what I'm doing. If you need to see the rest of the code, you can download it from the link to my blog I gave above. Thanks ...
-(void)drawAtPoint:(CGPoint)point withFont:(UIFont*)font
orientation:(WBOrientation)orient
{
CGContextRef ctxt = [self contextSave];
const std::string tbuf = toStdString(self, NSMacOSRomanStringEncoding);
CGAffineTransform tran = CGAffineTransformMake(1, 0, 0, -1, 0, 0);
switch (orient)
{
case WBOrientUp:
// nothing to do
break;
case WBOrientDown:
tran = CGAffineTransformRotate(tran, degreesToRadians(180.0));
break;
case WBOrientLeft:
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;
case WBOrientRight:
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;
default:
assert(false);
break;
}
contextFont(ctxt, font);
CGContextSetTextDrawingMode(ctxt, kCGTextFill);
CGContextSetTextMatrix(ctxt, tran);
CGContextSetTextPosition(ctxt, point.x, point.y);
CGContextShowText(ctxt, tbuf.c_str(), tbuf.length());
[self contextRestore:ctxt];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就在我自己做了几天后 =)
这就是我解决它的方法:
only a few days after i did it myself =)
this is how i solved it:
使用
UIGraphicsPushContext
函数,然后 使用 UIKit 绘制字符串,然后使用UIGraphicsPopContext
函数。AppKit (Mac) 解决方案本质上是相同的,使用 NSGraphicsContext 步骤 1 和 AppKit 的步骤 2 的 NSString 添加。对于
-[NSView drawRect:]
中的窗口上下文,步骤 3 消失(您将在步骤 1 中从 NSGraphicsPort 获取 CGContext,这意味着它已经设置) ;对于其他上下文,例如位图或 PDF 上下文,您将在步骤 1 和步骤 3 中使用相同的 NSGraphicsPort 方法 (setCurrentContext:
)。Set the CGContext as the current graphics context using the
UIGraphicsPushContext
function, then draw the string using UIKit, then pop the context back off the stack using theUIGraphicsPopContext
function.The AppKit (Mac) solution is essentially the same, using NSGraphicsContext for step 1 and AppKit's NSString additions for step 2. For window contexts in
-[NSView drawRect:]
, step 3 goes away (you would obtain the CGContext from the NSGraphicsPort in step 1, meaning it's already set); for other contexts, such as bitmap or PDF contexts, you would use the same NSGraphicsPort method (setCurrentContext:
) in both step 1 and step 3.