Question

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" );
}
Was it helpful?

Solution

I've used the following code without problem to draw text directly to a Quartz context:

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]));

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:

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

OTHER TIPS

Hey, I just found a note in my code.

Apparently CGContextShowTextAtPoint doesn't work with CGContextSetFont.
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.

Other people (here and here) have done some affine transformations and used the CGContextShowTextAtPoint method in order to make it work:

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);

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:

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

Hey, could be an encoding problem.

Try changing kCGEncodingFontSpecific to kCGEncodingMacRoman.

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

CGContextSetTextPosition( oContex, 100, 100 );

with

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

use CGContextSetTextMatrix method. The "upside down problem" is from matrix.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top