IPhone: how to draw text in a window?

A strange situation - examples from apple works, but after I changed them a little, the text does not appear. This bit of code correctly draws a 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" );
}
+5
source share
6 answers

I used the following code without problems to draw text directly in the context of quartz:

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 is not obvious what happens in your case. In this case, you need to pay attention to two things: the text can be inverted due to the inverted coordinate space of UIViews and their CALayers, and, as noted by Rhythmic Fistman, this does not handle UTF encodings.

, - - :

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

, .

- CGContextShowTextAtPoint CGContextSetFont.
CGContextSelectFont. , ?

, , , CGContextShowTextAtPoint.

, , .

+3

( ) 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);
+1

, .

kCGEncodingFontSpecific kCGEncodingMacRoman.

, , CGContextShowTextAtPoint.
, -unicode , API. UIKit .
unicode ,
.

+1

, , , . ,

CGContextSetTextPosition( oContex, 100, 100 );

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

use the CGContextSetTextMatrix method. Inverted Side Problem is the matrix.

+1
source

All Articles