Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: how to draw text in a window?

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" );
}
like image 388
grigoryvp Avatar asked Jul 12 '09 08:07

grigoryvp


People also ask

Is there a draw tool on iPhone?

Use the Notes app to draw a sketch or jot a handwritten note with your finger. You can choose from a variety of Markup tools and colors and draw straight lines with the ruler.

How do you add text to drawings on iPhone?

Tap Edit, then tap the Markup button . Tap the Add button to add text, shapes, and more.


3 Answers

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]];
like image 101
Brad Larson Avatar answered Nov 04 '22 23:11

Brad Larson


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.

like image 26
Rhythmic Fistman Avatar answered Nov 04 '22 23:11

Rhythmic Fistman


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);
like image 23
e.James Avatar answered Nov 05 '22 01:11

e.James