怪现状 - 从苹果的作品的例子,但之后,我改变他们一点,不显示文本。这段代码正确绘制蓝色背景,但拒绝在其上描绘文字,不管我做什么:

#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" );
}
有帮助吗?

解决方案

我用下面的代码没有问题直接绘制文本到石英上下文:

CGContextSetStrokeColorWithColor(context, strokeColor); 
CGContextSetFillColorWithColor(context, strokeColor);

CGContextSelectFont(context, "Helvetica", fontSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextPosition(context, 0.0f, round(fontSize / 4.0f));
CGContextShowText(context, [text UTF8String], strlen([text UTF8String]));

这不是很明显出现了什么问题,你的情况。有两件事情要注意在这种情况下:文本可能会得出倒置由于UIViews及其CALayers的翻转坐标空间,并作为艺术Fistman指出,这种不处理UTF编码

一个更好,尽管不太高性能,方法是这样做:

CGContextSetFillColorWithColor(context, strokeColor);
[text drawAtPoint:CGPointMake(0.0f, 0.0f) withFont:[UIFont fontWithName:@"Helvetica" size:fontSize]];

其他提示

嘿,我刚刚发现一张纸条在我的代码。

显然CGContextShowTextAtPointCGContextSetFont工作。结果 您需要改用CGContextSelectFont。明显,是吗?

不管怎样,我知道这是不是这样的,但你可以尝试使用CGContextShowTextAtPoint

这可能没有在一个单独的答案属于。

其他人(这里这里)已经做了一些仿射变换,并且为了使用的CGContextShowTextAtPoint方法使其工作:

CGContextSelectFont(oContex, @"Monaco", 10, kCGEncodingFontSpecific);
CGContextSetTextDrawingMode(oContex, kCGTextFill);
CGContextSetRGBFillColor(oContex, 1.0, 0.0, 0.0, 1.0);
CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(oContex, xform);
CGContextShowTextAtPoint(oContex, 100, 100, "abc", 3);

另一个可能的问题是,CGContextSetRGBFillColor方法采用浮点参数范围为0.0到1.0。您正在使用255表示“全彩”,这是该方法不期待什么。以下应该工作,如果是更舒服的0〜255范围:

CGContextSetRGBFillColor(oContex, 255/255.0, 0/255.0, 0/255.0, 1.0);

嗨,可能是一个编码的问题。

尝试改变kCGEncodingFontSpecifickCGEncodingMacRoman

后来,当你得到它的所有工作,摆脱CGContextShowTextAtPoint的。结果 这是垃圾,非Unicode,并使用你需要点击字形版本 呼叫禁止使用的API。你的方式最好使用UIKit的文本图。点击 这是unicode的精明和也做像样的字形替换为字符结果 中不存在在字体。

还有一种可能性,那就是文字位置您指定的矩形外在于你正试图借鉴。这很容易通过用测试此

CGContextSetTextPosition( oContex, 100, 100 );

CGContextSetTextPosition( oContex, CGRectGetMidX(i_poRect), CGRectGetMidY(i_poRect));

使用CGContextSetTextMatrix方法。的“颠倒问题”是从矩阵。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top