how to show words in the layer - iphone

I want to write some words to one layer. but when I create the layer,I can't find the corresponding API to attach the words to the layer.
layer2 = [CALayer layer];
[layer2 setBackgroundColor:[[UIColor grayColor] CGColor]];
layer2.bounds = CGRectMake(0, 0, 60,40);
layer2.position = CGPointMake(25,25);//
layer2.contentsRect = layer2.bounds;
layer2.contents=#"Hello World~~"; //It's nothing in the showing layer .
[self.layer addSublayer:layer2];

contents property works only with CGImageRef. If you wish to use text on it, use CATextLayer.
Example Usage
CATextLayer * textLayer = [CATextLayer layer];
textLayer.backgroundColor = [[UIColor darkGrayColor] CGColor];
textLayer.foregroundColor = [[UIColor whiteColor] CGColor];
textLayer.bounds = CGRectMake(0, 0, 60, 40);
textLayer.position = CGPointMake(25, 25);
textLayer.string = #"Hello World";
textLayer.font = CGFontCreateWithFontName(CFSTR("Helvetica"));
textLayer.fontSize = 15.0;
[self.window.layer addSublayer:textLayer];
Of course, I am leaking memory with CGFontCreateWithFontName. You can fix that by assigning it to a variable and releasing it later.

Related

UIView layer renderInContext not drawing to image context

I'm trying to draw a view's layer to a CGGraphicsImageContext using renderInContext, however the view layer is not displaying on the resultant UIImage.
UIGraphicsBeginImageContext(scaledImage.size);
[scaledImage drawInRect:CGRectMake(0, 0, scaledImage.size.width, scaledImage.size.height)];
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *croppedImage = [UIGraphicsGetImageFromCurrentImageContext() croppedImage:CGRectMake(0, 44, 320, scaledImage.size.height - (60 + 44))];
UIGraphicsEndImageContext();
My drawRect method for the view is as follows:
-(void)drawRect:(CGRect)rect
{
CALayer *background = [[CALayer alloc] init];
background.backgroundColor = [UIColor blackColor].CGColor;
background.opacity = 0.4;
background.frame = self.bounds;
[self.layer addSublayer:background];
CATextLayer *textLayer = [[CATextLayer alloc] init];
textLayer.string = _poseName;
textLayer.fontSize = 19;
textLayer.foregroundColor = [UIColor whiteColor].CGColor;
textLayer.alignmentMode = kCAAlignmentCenter;
textLayer.frame = self.bounds;
CGSize textSize = [_poseName sizeWithFont:[UIFont fontWithName:#"Helvetica" size:19]
constrainedToSize:textLayer.bounds.size
lineBreakMode:NSLineBreakByWordWrapping];
textLayer.position = CGPointMake(160, self.bounds.size.height - textSize.height/2);
[self.layer addSublayer:textLayer];
}
The view's layers are displaying correctly when the app is running however, the view is not rendering to the graphics context as I would expect. Thank you in advance for your help!

iphone-give transparent background to CAShapeLayer *maskLayer

I have a UIImageView on UIView.
On that I have drawn a shape using UIBezierPath. Now I want other part to be transparent.
My code is,
- (void)drawRect:(CGRect)rect
{
self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
aPath = [UIBezierPath bezierPath];
CGContextRef aRef ;
aRef = UIGraphicsGetCurrentContext();
[[UIColor clearColor] setStroke];
[[UIColor clearColor] setFill];
aPath.lineWidth = 2;
aPath = [UIBezierPath new];
//path is calculated here using addLineToPoint addArcWithCentre methods
[aPath fill];
[aPath stroke];
[self setClippingPath:aPath :imgV];
[[[UIColor clearColor] colorWithAlphaComponent:0.0] setFill];
CGContextFillRect(aRef, rect);
CGContextFillPath(aRef);
self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
}
- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView;
{
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imgView.frame;
maskLayer.path = [clippingPath CGPath];
maskLayer.backgroundColor = [[[UIColor clearColor] colorWithAlphaComponent:0.0] CGColor];
maskLayer.fillColor = [[UIColor whiteColor] CGColor];
[imgView removeFromSuperview];
imgView.layer.mask = maskLayer;
[self addSubview:imgView];
}
Output can be seen in following image. Black part around that image cur should be transparent
Please help me.
I think you might be missing one last call, probably right before addSubview:
imgView.clipsToBounds = YES;

rounded corners on UIPageViewController

I was wondering if anyone was ever successful in changing the corners of an UIPageViewController (the fancy book turning animation of iBooks) to rounded corners?
I tried this but to no avail:
[self.notebookPages setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.notebookPages.dataSource = self.pageModelController;
self.notebookPages.doubleSided = NO;
[self addChildViewController:self.notebookPages];
// mask
CAShapeLayer *maskLayer = [CAShapeLayer layer];
UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 332, 480)
byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight
cornerRadii:CGSizeMake(15.f, 15.f)];
maskLayer.fillColor = [[UIColor whiteColor] CGColor];
maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
maskLayer.path = [roundedPath CGPath];
self.notebookPages.view.layer.mask = maskLayer;
[self.notebookScrollNavigationController.notebook.pages addSubview:self.notebookPages.view];
self.notebookPages.view.frame = CGRectMake(0, 0, 332, 480);
[self.notebookPages didMoveToParentViewController:self];
This is all a bit hardcoded (which is obviously bad) but I was just trying to find out if rounded corners would work. However, I only get a non-rounded transparent corner:
Add this import:
#import <QuartzCore/QuartzCore.h>
Now you can use cornerRadius:
self.notebookPages.view.layer.cornerRadius = 6;

Glow Effect on rectangle in iPad

I have take a lot of patient and make the rectangle in glow effect as per my requirement.
CALayer *bevelLayer = [CALayer layer];
[bevelLayer setBounds:CGRectMake(0.0f, 0.0f, 300.0f, 300.0f)];
[bevelLayer setPosition:CGPointMake(300.0f, 550.0f)];
[bevelLayer setBackgroundColor:[[UIColor whiteColor] CGColor]];
[bevelLayer setShadowOpacity:1.0];
[bevelLayer setShadowRadius:7.0f];
[bevelLayer setShadowColor:[[UIColor colorWithRed:0.0f/255.0 green:126.0f/255.0f blue:255.0f/255.0f alpha:1.0f] CGColor]];
[bevelLayer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(-10.0f, -10.0f, 310.0f, 310.0f) cornerRadius:5.0f] CGPath]];
[[[self view] layer] addSublayer:bevelLayer];
Instead of adding a new layer, why dont you directly try it on self.view..
Also, shadowOpacity is a value between 0 and 1... So giving 10 is definitely not going to help you.
If you want to try in self.view, here is the code:
self.view.layer.shadowColor = [[UIColor greenColor] CGColor];
self.view.layer.shadowOffset = CGSizeMake(1, 1);
self.view.layer.shadowOpacity = 1;
self.view.layer.shadowRadius = 20;
This way you dont have to worry about the bounds of the layer..

Is it possible to add a background Image to a CAShape? (iPhone)

I am trying to distort an image in an arbitrary way, following for example a bezier shape. I have used CAShapeLayer to create the shape, but then it seems that setting its contents to an Image does not work. How can I have the image distorted following that shape? Is it even possible?
Thank you!
it is not possible to do so, but i succeeded in doing so by putting a CALayer on top a CAShapeLayer.
this is my code:
CALayer *image =[CALayer layer];
image.frame = CGRectMake(0, 0, 75, 75);
image.contents = (id) [UIImage imageNamed:#"number-1"].CGImage;
[self.layer addSublayer:image];
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 75, 75)];
blueCircleLayer = [CAShapeLayer layer];
blueCircleLayer.path = circle.CGPath;
blueCircleLayer.strokeColor = [UIColor blueColor].CGColor;
blueCircleLayer.fillColor = [UIColor clearColor].CGColor;
blueCircleLayer.shadowColor =[UIColor blackColor].CGColor;
blueCircleLayer.shadowOffset = CGSizeMake(0.0f, 5.0f);
blueCircleLayer.shadowOpacity = 0.7f;
blueCircleLayer.lineWidth = 7.0;
[self.layer addSublayer:blueCircleLayer];
This is currently not possible. I would suggest filing a radar (http://bugreport.apple.com) if you think this should be supported.
See also: Can a CGImage be added to the contents property of a CAShapeLayer?