tags:

views:

2344

answers:

5

Hello.

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" );
}
+1  A: 

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);
e.James
Changing 255 to 1.0f not helping.Adding transform not helping.
Eye of Hell
+1  A: 

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.

Rhythmic Fistman
Not working also.Btw, you can copy and paste my code, it will not need a project or something, just compile single .m file, run it and see for yourself :)
Eye of Hell
No compiler here. Have you tried this code in a more traditional project, say one with a .xib file?
Rhythmic Fistman
If this work with a .xib file and are not working without it, it's will be a devastating black magic. And for sure i must know WHY it's not working :(
Eye of Hell
+1  A: 

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]];
Brad Larson
[ @"abc" UTF8String ] not working either
Eye of Hell
btw, english text in UTF-8 is binary compatible with english text in ASCII :(.
Eye of Hell
+1  A: 

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.

Rhythmic Fistman
CGContextSelectFont is the function i'm using from start. It's all simply not working :(. And i can't find an apple example simple enought to degradate it into such code in meaningfull time.
Eye of Hell
A: 

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));
Elise van Looij