Inner shadow in a UILabel - iphone

What's the easiest way to draw an inner shadow in a UILabel, without using images?
I know you can use shadowColor and shadowOffset for drop shadows, but I didn't see any such properties for inner shadows.

Check out FXLabel. I've been using it recently and it works great.
https://github.com/nicklockwood/FXLabel

I do not believe there is any easy way to accomplish this appearance. You will either need to use an image or a custom view that draws the shadow.
I would use your favorite image editor to make an image of the inner shadow composited against transparency (or a color if you need it) and load it into a stretchable UIImage. For example here is an inner shadow image (17px x 17px):
You can load it like this:
UIImage* innerShadow = [ [ UIImage imageNamed: "innershadow.png" ]
stretchableImageWithLeftCapWidth: 8 topCapHeight: 8 ];
UIImageView* innerShadowView = [ [ [ UIImageView alloc ] initWithImage: innerShadow ]
autorelease ];
myLabel.backgroundColor = [ UIColor clearColor ];
Then place innerShadowView behind the label. You could generate the image programmatically as well if you wanted to avoid an image resource.
** Update based on comments follows **
Ah, based on your latest comment the effect you are looking for is completely different than the picture in the earlier comment.
In this case you will have to make a UILabel subclass and do your own drawing to composite the effect you are looking for. Take a look at this answer for how to composite the inner shadow effect.

What you mean by any such properties for inner shadows.
shadowOffset is the option to set the shadows (vertical or horizontal shadow) of the UILabel.
I do not know but this answer may be long to implement.

Related

Add shadow to a UITabbar in iOS8

I wanna add a simple shadow to my UITabbar.
I added a shadow image (a 10x1 gradient) to my project / related storyboard property.
The image should be repeated by itself, shouldn't it?
Nevertheless, there is no shadow in the design mode as well no shadow if I launch the application.
Would could be wrong?
This can be done programatically also:
self.tabBarController?.tabBar.layer.masksToBounds = false
self.tabBarController?.tabBar.layer.shadowColor = CustomColors.Graphites.dark.cgColor
self.tabBarController?.tabBar.layer.shadowOpacity = 0.8
self.tabBarController?.tabBar.layer.shadowOffset = CGSize(width: 0, height: -4.0)
self.tabBarController?.tabBar.layer.shadowRadius = 3
keep in mind:
read
understand
To use an image shadow you have to set a background image.
I used a simple transparent / bg-colored 16x16 image.
After that, the the shadow appeared.

CATiledLayer Drop Shadow Rendering Incorrect

I have a view that's rendering a PDF into a CATiledLayer. This is working well.
Now I'm attempting to add a drop shadow to the view, so I did the usual:
tiledLayer.masksToBounds = NO;
tiledLayer.shadowOffset = CGSizeMake(5, 5);
tiledLayer.shadowRadius = 5;
tiledLayer.shadowOpacity = 0.5;
tiledLayer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
But what I'm seeing rendered is incorrect. What seems to be happening is that there's a drop shadow drawn for each tile as the tiles are being drawn. Once all the tiles are drawn, the final product looks right and has a shadow in the right place, but the intermediate rendering is distracting.
How can I use a drop shadow with a CATiledLayer?
You should wrap your CATiledLayer in a container layer with those shadow attributes applied to it. Judging by the self.bounds call, you might already be embedding the CATiledLayer in a view’s layer, in which case (unless you need masksToBounds) you can just apply the shadow attributes to that layer directly.
It makes perfect sense that a drop shadow is being drawn for each tile, that is exactly what you told it to do. I assume you have your tiles loading in some kind of parent view that contains them (usually a UIScrollView). You should set your drop shadow on that containing view.

Custom UITextField

How would I go about creating a UITextField like the one in this image?
It appears to be slightly larger, specifically in height.
This can be done much better and simpler with a stretchable image:
UIImage *fieldBGImage = [[UIImage imageNamed:#"input.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
[myUITextField setBackground:fieldBGImage];
Think of the background of the text field as split into three sections. A middle section which can be stretched and caps on the ends. Create an image which only needs to be long enough to contain one pixel of this repeating section (a very short text field), and then create a stretchable image from it using stretchableImageWithLeftCapWidth: topCapHeight. Pass the width of the left end cap into 'leftCapWidth.' You can make it stretch vertically as well, but if your background image is the same height as your text box it wont have an effect.
If you're familiar with 9-slice scaling in Flash or Illustrator it's exactly the same concept, except the middle sections are only one pixel wide/tall.
The advantage of this is you don't have to worry about multiple layered objects scaling together and you can resize your text fields any time and the background will stay in tact. It works on other elements too!
You probably want to use the background and borderStyle properties of UITextField. Setting borderStyle as UITextBorderStyleNone and create a custom background image to be stretched and used as the background property would be one approach.
I suggest taking a look at those properties in the UITextField class reference.
You can do this by:
yourTextField.borderStyle = UITextBorderStyleNone;
yourTextField.background = [UIImage imageNamed:#"email-input.png"];
And if you want to give margin to your text inside the textfield, you can do this by:
// Setting Text Field Margins to display the user entered text Properly
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
yourTextField.leftView = paddingView;
yourTextField.leftViewMode = UITextFieldViewModeAlways;
You can also do this through Interface Builder.
just use the following line and it should work
textfield_name.background = [UIImage imageNamed : #"yourImage.png"];
here, "yourImage" is the background image you wanna set...
however, this will work only if your button isnt a roundrect button.So, you can change the type of the button in the Interface Builder or you can use
textfield_name.borderstyle = UITextBorderStyleNone or UITextBorderStyleBezel
and you r gud2go....!
Take an uiimageview set its image property to the image you want as uitextfield background. And on top of this uiimageview place an uitextfield with borderstyle none. This you can do directly in interface builder.

How can I apply UIColor to an existing image?

I have a1.png image.
I want to apply a color effect on that image.
By "apply a colour" I mean a foreground color with alpha < 1.
Does anyone have a solution?
I think you are looking for GLImageProcessing sample code
I haven't get solution for applying UIColor to existing image.
One alternative way is that instead of applying color to an image you can take a View and apply color to the view.
UIColor *color = [del color];
[perceptView setBackgroundColor:[color colorWithAlphaComponent:0.5]];
[perceptView setNeedsDisplay];

How do I use a mask to punch out my image?

I have a mask (loaded from a 256 grey PNG) that I want to apply to an image that's being used as part of my process for drawing a UITableViewCell's imageView.image property.
When the cell isn't selected/highlighted, I CGImageCreateWithMask with a square of the proper color and the mask, then drawAtPoint: it into the image I'm building. This works fine.
However, when the cell is selected or highlighted, I'd like to instead use the mask to instead punch through my image appropriately. That is, when my mask specifies full opacity, I want the image I'm building to be completely transparent so the tableview's background is drawn through it. Where my mask specifies 0 opacity, I want the alpha channel untouched. I want nothing other than the alpha channel affected.
I guess what I mean is that I want to draw clearColor over a UIImage, with a varying level of opacity according to a mask.
First, what is this called? And second, how do I do it?
I think you have to manipulate the CALayers for that. You can use the mask property of the cell's CALayer : CALayer mask attribute.
That is, something in the way of (if myMask is descendent of UIView) :
myCell.layer.mask = myMask.layer