• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Get text width outside a view
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Get text width outside a view


  • Subject: Get text width outside a view
  • From: "Christian Giordano" <email@hidden>
  • Date: Wed, 22 Oct 2008 09:57:54 +0100

I'm trying to calculate the width of a text and the same code I used
in the past in a drawRect routine in a view to its
UIGraphicsGetCurrentContext() isn't working. The main difference of
course is the context, in this case I created a bitmap context and the
code is basically this:

        CGFloat fontSize = 20.0;
        int cw = 500;
	int ch = 50;
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
	CGContextRef context = CGBitmapContextCreate(NULL, cw, ch, 8, cw,
colorSpace, kCGRenderingIntentDefault);
	if(context == NULL){
		NSLog(@"Paragraph couldn't create context!");
	}
	CGColorSpaceRelease(colorSpace);
	CGContextSelectFont(context, "Helvetica", fontSize, kCGEncodingMacRoman);
	CGContextSetTextDrawingMode(context, kCGTextFill);

	const char* textC = [str cStringUsingEncoding:NSMacOSRomanStringEncoding];
	int len = strlen(textC);
	CGContextShowTextAtPoint(context, 0, 0, textC, len);
	CGPoint pt = CGContextGetTextPosition(context);

        CGContextRelease(context);

pt.x is 0.

I used a bitmap context because I didn't know how to create a
different one, isn't possible to write text on a bitmap context or I
am missing something else?


Thanks a lot for any help, chr
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Get text width outside a view
      • From: Marco Masser <email@hidden>
  • Prev by Date: Re: Putting an image on a CALayer
  • Next by Date: Re: When does Cocoa fall apart? (Run loops)
  • Previous by thread: Re: When does Cocoa fall apart? (Run loops)
  • Next by thread: Re: Get text width outside a view
  • Index(es):
    • Date
    • Thread