Get text width outside a view
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