How to set 3d effect to UIButton - ios

Hi i am try to set 3d effect to UIButton but my code does n't set properly.
it's come like this
but my requirement is like below
could you please help me some one.
My code is below:
self.submitBtn1.backgroundColor = [self colorWithHexString:#"a53129"];
self.submitBtn1.layer.cornerRadius = 3.0;
self.submitBtn1.layer.borderWidth = 2.0;
self.submitBtn1.layer.borderColor = [[UIColor clearColor] CGColor];

Use the following code to achieve this, set button type to custom
self.submitBtn.backgroundColor = [UIColor colorWithRed:(200.0f/255.0f) green:0.0 blue:0.0 alpha:1.0];
self.submitBtn.layer.cornerRadius = 3.0;
self.submitBtn.layer.borderWidth = 2.0;
self.submitBtn.layer.borderColor = [[UIColor clearColor] CGColor];
self.submitBtn.layer.shadowColor = [UIColor colorWithRed:(100.0f/255.0f) green:0.0 blue:0.0 alpha:1.0].CGColor;
self.submitBtn.layer.shadowOpacity = 1.0f;
self.submitBtn.layer.shadowRadius = 1.0f;
self.submitBtn.layer.shadowOffset = CGSizeMake(0, 3);
I am attaching image, shows result of this code.

Try playing around with the layer's shadow parameters. Like so:
self.submitBtn1.layer.shadowRadius = 5
self.submitBtn1.layer.shadowOpacity = 0.8
self.submitBtn1.layer.shadowOffset = CGSize(width: 5, height: 5)
Oh, and of course...
submitBtn1.setTitle(submitBtn1.currentTitle?.uppercaseString, forState: .Normal)
;)

Shadow is what makes the button look like a 3D object. Use this code below to get shadow for 3D like effect. Change to desired values, as per your requirements.
submitBtn1.layer.shadowRadius = 5
submitBtn1.layer.shadowOpacity = 0.8
submitBtn1.layer.shadowOffset = CGSize(width: 5, height: 5)

Related

How to do colour shading in Objective-C

I need to do a colour shading in objective c. As shown in this image shade is on the button of HardService and SoftService.
Is there is any way to do this ? Thanks in Advance!
CAGradientLayer would server your purpose - Reference:
http://blog.apoorvmote.com/gradient-background-uiview-ios-swift/
You can use either a gradient image or CAGradientLayer for this purpose
CAGradientLayer added to an existing button:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = button.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[button.layer insertSublayer:gradient atIndex:0];
You can use an image for showing shadow. Use blur image or change alpha of the imageview.
Or if you want to do it programmatically, try it:
Obj c:
//create elliptical shadow for button through UIBezierPath
CGRect ovalRect = button.bounds;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ovalRect];
//applying shadow to path
button.layer.shadowColor = [UIColor yourcolor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0, 0.0);
button.layer.shadowOpacity = 1.0;
button.layer.shadowRadius = 3.0;
button.layer.shadowPath = path.CGPath;
Swift :
//create elliptical shdow forimage through UIBezierPath
var ovalRect = button.bounds
var path = UIBezierPath(ovalInRect: ovalRect)
//applying shadow to path
button.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).CGColor
button.layer.shadowOffset = CGSizeMake(0.0, 0.0)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 3.0
button.layer.shadowPath = path.CGPath
Taken from http://www.innofied.com/implementing-shadow-ios/ and also have a look to know more: UIView with rounded corners and drop shadow?
My another answer related to this: Drop shadow in ios

How to draw an outline around a column?

I would like to draw an outline around a column. here is how my app actually looks like: https://gyazo.com/80dffe6a153e82565a610222a43e0c11
So the white column represent the day on where we are actually. But I don't really want to do that.
Actually I'm doing this:
if(column == _intOfTheDay){ //intOfTheDay represent the day one where we are actually
cell.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.95];
}
I've tried something like this to get closer of the result that I want:
if(column == _intOfTheDay){
cell.layer.borderWidth = 5.0f;
cell.layer.borderColor = [UIColor redColor].CGColor;
}
But the problem here (without all the bugs due to the UICollectionView) is that it draw a rectangle around each cell. And what I would like is to draw only one rectangle but around all the column.
How could I process?
Every help will be appreciated, thanks in advance.
If you don't want to subclass the UICollectionViewCell then use this :
float borderWidth = 5.0;
CALayer *leftBorder = [CALayer new];
leftBorder.frame = CGRectMake(0.0, 0.0, borderWidth, cell.frame.size.height);
leftBorder.backgroundColor = [UIColor redColor].CGColor;
[cell.layer addSublayer:leftBorder];
CALayer *rightBorder = [CALayer new];
rightBorder.frame = CGRectMake(cell.frame.size.width - borderWidth, 0.0, borderWidth, cell.frame.size.height);
rightBorder.backgroundColor = [UIColor redColor].CGColor;
[cell.layer addSublayer:rightBorder];
You can do something like this, set three border to first cell of column- left,right and top and for last cell of column left,right and bottom and for all middle cell left and right. you can use CALayer to set one side border like below example,
CALayer *TopBorder = [CALayer layer];
TopBorder.frame = CGRectMake(0.0f, 0.0f, self.myImageView.frame.size.width, 3.0f);
TopBorder.backgroundColor = [UIColor redColor].CGColor;
[cell.layer addSublayer:TopBorder];

How to make iOS 8 shadow darker?

I have used an iOS 8 shadow property, and it's not dark enough. Here is my code:
View.layer.shadowOffset = CGSizeMake(1, 1);
View.layer.shadowColor = [[UIColor blackColor] CGColor];
View.layer.shadowRadius = 6.0f;
View.layer.shadowOpacity = 1.0f;
I need to make the shadow darker, so that it's almost black. And, is there a way to create a shadow or override the default property programmatically?

How to create transparent UIView with non-transparent border

I am trying to make a transparent UIView with a border. The problem is that the border will always be transparent, how can I make it non-transparent?
This is my code
- (void) viewDidLoad:(BOOL)animated
{
[super viewDidLoad:animated];
_transparentView.alpha = 0.5f;
_transparentView.layer.borderColor = [UIColor whiteColor].CGColor;
_transparentView.layer.borderWidth = 2.0f;
}
You can do it by adding transparent view to as a sub-view of another view like as bellow
_transparentView.alpha = 0.5f;
_MainView.backgroundColor = [UIColor clearColor];
_MainView.layer.borderColor = [UIColor whiteColor].CGColor;
_MainView.layer.borderWidth = 2.0f;
[_MainView addSubview:_transparentView];
You can achieve this by setting the backgroundColor of the view to a transparent color:
_transparentView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.5];
_transparentView.layer.borderColor = [UIColor whiteColor].CGColor;
_transparentView.layer.borderWidth = 2.0f;
This will make the background of the view a transparent green.
Just set the background to a clear color:
// set the border color
_transparentView.layer.borderColor = [UIColor whiteColor];
// set the border width
_transparentView.layer.borderWidth = 3.0f;
// want rounded corners? set this
_transparentView.layer.cornerRadius = 5.0f;
// make the background clear color to be fully transparent
_transparentView.backgroundColor = [UIColor clearColor];
You don't need subviews or anything else.
-(void)setRoundedView:(UIView *)vW{
CALayer *image = vW.layer;
[image setCornerRadius:5];
[image setBorderWidth:1];
image.masksToBounds = YES;
image.borderColor = [UIColor colorWithRed:202.0/255.0 green:202.0/255.0 blue:202.0/255.0 alpha:1].CGColor;
}

text with sharp shadow around

I want to achieve the text appearance like in the pictures below:
Now I'm working on shadow around the letters and I can't figure out how to do that.
What I've tried so far:
- The solution with:
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(1, 2);
gives a nice sharp shadow, but it doesn't fit my needs by two reasons:
It gives a shadow only from one side, set up by shadowOffset, whereas I need a "wrapping" shadow;
This solution doesn't give the soft part of the shadow (gradient) as there is in the pictures;
-The solution with:
label.layer.shadowOffset = CGSizeMake(0, 0);
label.layer.shadowRadius = 5.0;
label.layer.shouldRasterize = YES;
label.layer.shadowOpacity = 1;
label.layer.shadowColor = [UIColor blackColor].CGColor;
label.layer.masksToBounds = NO;
Works great, but it gives too soft shadow even though the shadowOpacity is set to 1 and the shadowColor is set to black:
Obviously it's not enough and I already think about drawing in labels' context. But it is not clear to me how would I achieve the goal even through context drawing.
Any idea would be much appreciated.
Try this
Create a Custom UILabel SubClass and Override the following method
- (void)drawTextInRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetShadow(context, CGSizeMake(0.0, 0.0), 10);
CGContextSetShadowWithColor(context, CGSizeMake(0.0, 0.0), 10, [UIColor blackColor].CGColor);
[super drawTextInRect:rect];
CGContextRestoreGState(context);
}
and this bottomColorLayer to the Label
CALayer *bottomColorLayer = [CALayer layer];
bottomColorLayer.frame = CGRectMake(0, labelRect.size.height/2, labelRect.size.width, labelRect.size.height/2);
bottomColorLayer.backgroundColor = [[UIColor colorWithWhite:0 alpha:.5] CGColor];
[label.layer insertSublayer:bottomColorLayer above:(CALayer *)label.layer];
or If you want Gradient
CAGradientLayer *bottomGradient = [CAGradientLayer layer];
bottomGradient.frame = CGRectMake(0, labelRect.size.height/2, labelRect.size.width, labelRect.size.height/2);
bottomGradient.cornerRadius = 0.0f;
bottomGradient.colors = #[(id)[[UIColor colorWithWhite:1 alpha:.5] CGColor], (id)[[UIColor colorWithWhite:0 alpha:.5] CGColor]];
[label.layer insertSublayer:bottomGradient above:(CALayer *)label.layer];
Use an explicit shadow path that's the shape you want. It sounds like you want a shadow the same shape as your text, but larger, with each shadow-letter centered on its corresponding letter. Once you have that, use the shadow radius to soften the edges of the shadow to whatever degree you want.
The code you have relies on the shadow radius alone to make the shadow larger than the text, which removes your ability to control the softness.
Try with the code below :-
[_testLable setText:#"TION ERRO"];
[_testLable setTextColor:[UIColor whiteColor]];
[_testLable setFont:[UIFont boldSystemFontOfSize:22] ];
_testLable.layer.shadowOffset = CGSizeMake(0, 0);
_testLable.layer.shadowRadius = 10.0;
_testLable.layer.shouldRasterize = YES;
_testLable.layer.shadowOpacity = 1;
_testLable.layer.shadowColor = [UIColor blackColor].CGColor;
_testLable.layer.masksToBounds = NO;
CGPathRef shadowPath = CGPathCreateWithRect(_testLable.bounds, NULL);
_testLable.layer.shadowPath = shadowPath;
CGPathRelease(shadowPath);
Its output is like this:-

Resources