What is UILabel default shadowRadius? - ios

I use QuartzCore to set UILabel shadows in UITableViewCell:
cell.textLabel.layer.shadowColor = [[UIColor orange] CGColor];
cell.textLabel.layer.shadowOffset = CGSizeMake(0.0, 1.0);
cell.textLabel.layer.shadowRadius = 0.0;
cell.textLabel.layer.masksToBounds = NO;
But because of it slow performance, I have to implement -(void)drawRect:(CGRect)rect method.
Recently I found out that shadow for UILabel can be set with it properties:
cell.textLabel.shadowColor = [UIColor orangeColor];
cell.textLabel.shadowOffset = CGSizeMake(0.0, 1.0);
Now I want delete drawRect: implementation and just set shadows through properties.
QuartzCore CALayer shadowRadius default value is 3.0.
Though UILabel has not such property, is it mean that shadow radius equauls 0.0?

The documentation for UILabel says:
Text shadows are drawn with the specified offset and color and no blurring.
So, yes, the shadow radius is 0.

just a advise - setting shadow using QuartzCore in a UITableView cell impacts scroll performance. it would definitely not be smooth scrolling.
I suggest you use a UIImageView just below the UILabel to give the same effect. Its probably more work to keep the shadow-image updated with changes in design but if its a trade-off between reducing developer effort or improving user-experience; I would incline towards the latter 9 out of 10 times...

Related

iOS - CALayer setShadowColor not working

I need to set a bottom shadow to a button. I have the shadow but somehow I cannot change the shadow color. What am I doing wrong? Here is the code:
[self.buttonsBar.layer setShadowColor:[UIColor colorWithRed:227.0f/255 green:233.0f/255 blue:239.0f/255 alpha:1.0f].CGColor];
[self.buttonsBar.layer setShadowOpacity:1];
[self.buttonsBar.layer setShadowRadius:1];
[self.buttonsBar.layer setShadowOffset:CGSizeMake(0, 1)];
Make sure your self.buttonsBar.clipsToBounds = NO;
also make sure your self.buttonsBar.layer.masksToBounds = NO;
first try large radius and offset to make sure you see them then reduce the numbers, sometimes it is not easy to see the shadow depending on the background colors ...

How do I make a UISwitch under iOS 7 not take the background colour of the view behind it?

It looks like this whenever off:
While I'd prefer more of a grey background. Do I really have to use a UIImageView?
Here is how I changed the fill color of my iOS7 UISwitch.
First you need to import QuartzCore.
#import <QuartzCore/QuartzCore.h>
Then set the background color and round the UISwitch's corners.
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
[self addSubview:mySwitch];
This will give you a UISwitch with a custom off (background) color.
Hope this helps someone:)
You can set the setOnTintColor property of your UISwitch to the color you desire.
You can also set this for the switch in Interface Builder. Just set the background colour of the UISwitch to whatever colour you want (white, in the example below), then set a User Defined Runtime Attribute of layer.cornerRadius = 16:
There's no API support for changing the off fill color of a UISwitch.
Adjusting the tintColor will only affect the outline, and adjusting the backgroundColor will affect the whole frame, including the parts outside the rounded bounds.
You either have to place a properly shaped opaque UIView behind it or - easier - use a custom open source implementation, such as MBSwitch, which allows you to set the off fill color.
You can also use an image as background, using the [UIColor colorWithPatternImage];
mySwitch.onTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-on"]];
mySwitch.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"toggle-bg-off"]];
Adding to Barry Wyckoff solution : set tint color also
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 51.0, 31.0)];
mySwitch.backgroundColor = [UIColor redColor];
mySwitch.layer.cornerRadius = 16.0; // you must import QuartzCore to do this.
mySwitch.tintColor = [UIColor redColor];
[self addSubview:mySwitch];

Giving a shadow cause to slowly scroll down

I have white background and white label on it, so I need to give a shadow all side of characters I use this code and it works! but it cause to slowly scroll do you have any idea how can solve this problem?
cell.L_name.layer.shadowOpacity = 1.0;
cell.L_name.layer.shadowRadius = 1.0;
cell.L_name.layer.shadowColor = [UIColor blackColor].CGColor;
cell.L_name.layer.shadowOffset = CGSizeMake(1.0, 1.0);
try setting
cell.L_name.layer.shouldRasterize = YES;
this will render your label as a bitmap and might improve scrolling smoothness
additionally you could set
cell.opaque = YES;
which will optimize drawing performance as well.
Setting a shadowPath for the layer would probably increase the performance greatly.
cell.L_name.layer.shadowPath = [UIBezierPath pathWithRect:L_name.bounds].CGPath;
Besides, you can cache the generated shadow as a bitmap, which also increases the performance considerably. Do not forget to set rasterizationScale for retina displays.
cell.L_name.layer.shouldRasterize = YES;
cell.L_name.layer.rasterizationScale = [UIScreen mainScreen].scale;

Tableview scrolling lack of performance using cornerRadius, whats alternatives?

I can't find a solution for my issue, so I hope some experts could help me.
I have tableview with lots of cells. For each cell I have an image view with rounded corners:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *exampleView;
...
exampleView.layer.cornerRadius = 5.f;
exampleView.clipsToBounds = YES;
[cell.contentView addSubview:exampleView];
...
}
Resulting in a big lack of scrolling performance issue - when using cornerRadius and clipsToBounds = YES, any alternative solutions?
(The rounded corner image view contains pictures, and if the corner isn't set. The scrolling is smooth. So the problem is here)
EDIT #1: to clarify that I have already read other posts:
...
exampleView.frame = CGRectMake(5.0f,size.height-15.0f,30.0f,30.0f);
exampleView.layer.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor;
exampleView.layer.cornerRadius = 5.0f;
exampleView.layer.borderWidth = 0.3f;
exampleView.layer.borderColor = [UIColor blackColor].CGColor;
exampleView.layer.masksToBounds = NO;
exampleView.layer.shouldRasterize = YES;
//exampleView.clipsToBounds = NO;
[exampleView setImage: myImage ];
EDIT #2:
It's like myImage is lapping over the rounded corner view. How do I make the image fit the rounded corner view correct? or am I missing something?
Just change the following line
exampleView.layer.masksToBounds = YES;
To round out the corners in your imageView. See images below.
The tableview scrolling is a issue with view compositing. In addition to suggestions above, this will certainly help -
Set the opaque property of the UITableViewCell's contentView property, and backgroundView property. Go:
[cell.contentView setOpaque:YES];
[cell.backgroundView setOpaque:YES];
This will help reduce the view compositing effort at the earliest possible stage.
There are additional, advanced, techniques which would require you to use asynchronous processing to process your images as you scroll. They may be overkill if the above techniques work.

set label border in iphone

How can I set label's border which is dynamically generated (Not from Interface Builder)?
you can do it by
Label.layer.borderColor = [UIColor whiteColor].CGColor;
Label.layer.borderWidth = 4.0;
before this you need to import a framework QuartzCore/QuartzCore.h
Swift version
Set label border
label.layer.borderWidth = 2.0
Set border color
label.layer.borderColor = UIColor.blueColor().CGColor
Use rounded corners
label.layer.cornerRadius = 8
Make background color stay within rounded corners
label.layer.masksToBounds = true
You can also try to subclass your label and override the drawRect: method to draw or a border or whatever you like:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor blackColor] setStroke];
CGContextStrokeRect(context, self.bounds);
}
I'm not sure you can by default with UILabel. You might want to consider using a read-only (field.editing = NO) UITextField and setting it's borderStyle (which can be done programmatically using a UITextBorderStyle). That may be a little 'heavy' though. Another option may be to sub-class UILabel to draw your border.
Alternatively, and depending on your needs this may be better, use the backing CALayer and draw a border using it's borderColor and borderWidth properties.

Resources