iPhone - Draw some text with CGContext : ok but... mirrored - iphone

When I draw some text using CGContext, it is drawn mirrored.
I tried to apply some transformations, then it is draw well, but then the rest of the drawing and all coordinates seems to be draw bad.
I tried to save and restore the context, before and ater drawing the text (and aplying transformation), but that does not help.
How some text must be drawn onto a View using CGContext without affecting the rest of the drawing, nor the onscreen CGPoint coords for that text ?

Can you clarify what you mean as 'mirrored'? Here is some code for drawing some black text. It should not be 'mirrored'.
CGRect viewBounds = self.bounds;
CGContextTranslateCTM(ctx, 0, viewBounds.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextSetRGBFillColor(ctx, 0.0, 1.0, 0.0, 1.0);
CGContextSetLineWidth(ctx, 2.0);
CGContextSelectFont(ctx, "Helvetica", 10.0, kCGEncodingMacRoman);
CGContextSetCharacterSpacing(ctx, 1.7);
CGContextSetTextDrawingMode(ctx, kCGTextFill);
CGContextShowTextAtPoint(ctx, 100.0, 100.0, "SOME TEXT", 9);

I think you have to reverse the text matrix :
CGAffineTransform transform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, transform);

Related

How to get diagram with images as a boarder line in iOS?

I drew rectangle diagram with boarder dotted blue line. I would like to rectangle diagram with same images as a boarder line. That means i want to replace dots with images
My Code:
- (void)drawRect:(CGRect)rect
{
context =UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGFloat dashPattern[10] = {3.0, 2.0};
CGContextSetLineDash(context, 0, dashPattern, 2);
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextAddRect(context, self.bounds);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextClosePath(context);
CGContextStrokePath(context);
}
Can i set image instead of dots? Please give any suggestions to me. Thanks in advance.
I don't try yet, but maybe you can use:
CGContextSetStrokeColorWithColor(context, [UIColor colorWithPatternImage:myImage].CGColor);

Drawing a line with a shadow, but only want to keep shadow. IOS

I am trying to draw a line with a shadow but I do not want to keep the line but only the shadow.
I have attempted to set the stroke color of the line to clear but when I do that the shadow also disappears.
The following code creates 2 lines, i only want to keep the shadow because it looks nicer and its not pixelated like the line.
Is this possible?
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
CGColorRef shadowColor = CGColorCreate(colorSpace, components);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(10,10), 4.0, shadowColor);
Thanks.
So you can't do this directly because the shadow reflects the path, so if you make the path transparent, the shadow will also be transparent. There are a few workarounds I can think of (depends on exactly what you're doing) but one sneaky way would just be to draw the shadow far enough below the path that you can just draw over it, basically erasing it. For example, if the background is white, this will accomplish what you want:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
CGColorRef shadowColor = CGColorCreate(colorSpace, components);
CGContextSetShadowWithColor(context, CGSizeMake(0.0f,20.0f), 4.0, shadowColor);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);
}
hope this helps.
why dont you just draw your line as a shadow. Dont have the shadow drawn but instead draw a black line with .25 or less alpha and offset it to where you would expect the shadow to be.

Pop translations used during CGContext

As you can see in the code below I do some transformations on my UIView before writing some text. It seems though that the position of the text is affected by the aforementioned transformations.
Is there any way to 'pop' these displacements so I can then write my text in relation to the original 0,0 coordinate?
//turn PDF upsidedown
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(100, aUIView.bounds.size.height+300);
transform = CGAffineTransformScale(transform, 0.5, -0.5);
CGContextConcatCTM(pdfContext, transform);
// Draw view into PDF
// Is renderInContext deprecated? Something to look into.
[aUIView.layer renderInContext:pdfContext];
CGContextSelectFont (pdfContext, "Helvetica", 14, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
CGContextSetTextMatrix(pdfContext, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));
const char *text = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG TEST 1";
CGContextShowTextAtPoint (pdfContext, 10.0, 10.0, text, strlen(text));
I think CGContextSaveGState(cntx) and CGContextRestoreGState(cntx) are what you are looking for.

How to draw string in other device orientations using CGContext iOS

I'm trying to draw some text on the iPhone, but in landscape mode, where the home button is on the left. So I need to somehow twist the numbers (I'm guessing I use a transform) ?
Here's what I have now:
CGContextSetRGBFillColor(context, 1.0, 0, 0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(M_PI/2));
CGContextSetTextDrawingMode(context, kCGTextFill);
NSString *t = [NSString stringWithFormat:#"%i",seconds];
const char *str=[t UTF8String];
CGContextShowTextAtPoint(context,6.0,15.0,str,strlen(str));
What do I need to add to rotate the text so that it can be read when the phone is landscape with the home button on the left.
You have to apply two transformation matrices. The first one will flip the text and the second one will rotate it. Try the following code:
CGContextSetRGBFillColor(context, 1.0, 0, 0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);
CGAffineTransform xform = CGAffineTransformMake(
1.0, 0.0,
0.0, -1.0,
0.0, 0.0);
CGContextSetTextMatrix(context, xform);
CGContextConcatCTM(context, CGAffineTransformMakeRotation(M_PI_2));
CGContextSetTextDrawingMode(context, kCGTextFill);
NSString *t = [NSString stringWithFormat:#"%i",seconds];
const char *str=[t UTF8String];
CGContextShowTextAtPoint(context,6.0,15.0,str,strlen(str));

Why is my text flip over when using CGContextShowAtPoint?

I am writing a simple practice. However, my text is flip upside down when I trying to use CGContext to put some string on the UIView, I am wondering why and how to change it into correct format.
Here is my code in the drawRect
char *string = "TEST";
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSelectFont (context,"Helvetica-Bold",12, kCGEncodingMacRoman);
CGContextShowTextAtPoint(context, 5, 5, string, strlen(string));
CGContextClosePath(context);
Thanx for your help.
CoreGraphics uses cartesian coordinates, so you need to translate your context before doing any drawing
CGContextRef context = UIGraphicsGetCurrentContext();
// transforming context
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// your drawing code
Quartz2D has an inverted y axis - handy eh? If you are inside a drawRect method, you can use the following to flip the text over.
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
Another way is;
transform = CGAffineTransformMake(1.0,0.0,0.0,-1.0,0.0,0.0);
CGContextSetTextMatrix(context, transform);
Or on one line;
CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));
Use the UIKit additions in NSString:
NSString *string = #"TEST";
[string drawAtPoint:CGPointMake(5, 5)
withFont:[UIFont boldSystemFontOfSize:12]];