iPhone:如何在窗口中绘制文本?
奇怪的情况 - 苹果的示例有效,但在我稍微改变它们之后,文本不显示。 这段代码正确地绘制了蓝色背景,但无论我做什么都拒绝在其上绘制文本:
#import <UIKit/UIKit.h>
@interface CWnd : UIWindow @end
@implementation CWnd
- (void) drawRect : (CGRect) i_poRect
{
// This is working : windows is blue.
CGContextRef oContex = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor( oContex, 0, 0, 255, 1 );
CGContextFillRect( oContex, i_poRect );
// This is not working : not text is displayed.
CGContextSelectFont( oContex, "Monaco", 10, kCGEncodingFontSpecific );
CGContextSetRGBStrokeColor( oContex, 255, 0, 0, 1 );
CGContextSetRGBFillColor( oContex, 255, 0, 0, 1 );
CGContextSetTextDrawingMode( oContex, kCGTextFill );
CGContextSetTextPosition( oContex, 100, 100 );
CGContextShowText( oContex, "abc", 3 );
}
@end
@interface CDelegate : NSObject <UIApplicationDelegate> @end
@implementation CDelegate
- (void)applicationDidFinishLaunching : (UIApplication *) i_poApp
{
CGRect oRect = [ [ UIScreen mainScreen ] bounds ];
[ [ [ CWnd alloc] initWithFrame : oRect ] makeKeyAndVisible ];
}
@end
int main(int argc, char *argv[])
{
return UIApplicationMain( argc, argv, nil, @"CDelegate" );
}
Strange situation - examples from apple works, but after i change them a bit, text is not displayed. This bit of code correctly draws blue background but refuses to draw text on it no matter what i do:
#import <UIKit/UIKit.h>
@interface CWnd : UIWindow @end
@implementation CWnd
- (void) drawRect : (CGRect) i_poRect
{
// This is working : windows is blue.
CGContextRef oContex = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor( oContex, 0, 0, 255, 1 );
CGContextFillRect( oContex, i_poRect );
// This is not working : not text is displayed.
CGContextSelectFont( oContex, "Monaco", 10, kCGEncodingFontSpecific );
CGContextSetRGBStrokeColor( oContex, 255, 0, 0, 1 );
CGContextSetRGBFillColor( oContex, 255, 0, 0, 1 );
CGContextSetTextDrawingMode( oContex, kCGTextFill );
CGContextSetTextPosition( oContex, 100, 100 );
CGContextShowText( oContex, "abc", 3 );
}
@end
@interface CDelegate : NSObject <UIApplicationDelegate> @end
@implementation CDelegate
- (void)applicationDidFinishLaunching : (UIApplication *) i_poApp
{
CGRect oRect = [ [ UIScreen mainScreen ] bounds ];
[ [ [ CWnd alloc] initWithFrame : oRect ] makeKeyAndVisible ];
}
@end
int main(int argc, char *argv[])
{
return UIApplicationMain( argc, argv, nil, @"CDelegate" );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我已经使用以下代码毫无问题地将文本直接绘制到 Quartz 上下文:
您的情况出现问题并不明显。 在这种情况下,有两件事需要注意:由于 UIView 及其 CALayers 的坐标空间翻转,文本可能会颠倒绘制,并且正如 Rhythmic Fistman 指出的那样,这不处理 UTF 编码。
更好但性能较差的方法是执行以下操作:
I've used the following code without problem to draw text directly to a Quartz context:
It's not obvious what's going wrong in your case. There are two things to watch out for in this case: the text may draw upside-down due to the flipped coordinate space of UIViews and their CALayers, and as Rhythmic Fistman points out, this doesn't handle UTF encodings.
A better, although less performant, approach is to do something like:
嘿,我刚刚在我的代码中发现了一条注释。
显然
CGContextShowTextAtPoint
不适用于CGContextSetFont
。您需要改用
CGContextSelectFont
。 很明显,是吧?无论如何,我知道情况并非如此,但您可以尝试使用
CGContextShowTextAtPoint
。这可能不属于单独的答案。
Hey, I just found a note in my code.
Apparently
CGContextShowTextAtPoint
doesn't work withCGContextSetFont
.You need to instead use
CGContextSelectFont
. Obvious, huh?Anyway, I know this isn't the case, but you could try using
CGContextShowTextAtPoint
.This probably didn't belong in a separate answer.
其他人(此处和此处)完成了一些仿射变换并使用了
CGContextShowTextAtPoint
方法以使其正常工作:另一个可能的问题是 CGContextSetRGBFillColor 方法采用 0.0 到 1.0 范围内的浮点参数。 您使用 255 来表示“全彩”,这不是该方法所期望的。 如果您更喜欢 0 到 255 的范围,则以下内容应该有效:
Other people (here and here) have done some affine transformations and used the
CGContextShowTextAtPoint
method in order to make it work:One other possible problem is that the
CGContextSetRGBFillColor
method takes floating-point arguments in the range 0.0 to 1.0. You are using 255 to indicate "full color", which is not what that method expects. The following should work, if you are more comfortable with the 0 to 255 range:嘿嘿,可能是编码问题。
尝试将
kCGEncodingFontSpecific
更改为kCGEncodingMacRoman
。稍后,当您一切正常后,请摆脱
CGContextShowTextAtPoint
。它是垃圾,非 unicode,要使用您需要的字形版本
调用禁止的 API。 使用 UIKit 进行文本绘制会更好。
它精通 unicode,并且还可以对字符进行适当的字形替换
您的字体中不存在这些内容。
Hey, could be an encoding problem.
Try changing
kCGEncodingFontSpecific
tokCGEncodingMacRoman
.Later when you get it all working, get rid of
CGContextShowTextAtPoint
.It's garbage, non-unicode and to use the glyph versions you'll need to
call forbidden APIs. You're way better off using UIKit for text drawing.
It's unicode savvy and also does decent glyph substitution for characters
that aren't present in your font.
还有另一种可能性,那就是您指定的文本位置位于您尝试绘制的矩形之外。 替换可以很容易地测试这一点
通过
There is one other possibility and that is that the text position you specify lies outside the rectangle in which you are trying to draw. It's easy to test this by substituting
with
使用 CGContextSetTextMatrix 方法。 “颠倒问题”来自矩阵。
use CGContextSetTextMatrix method. The "upside down problem" is from matrix.