I have this code so far, but I just want a shadow at the bottom of a myView
myView.layer.shadowColor = [UIColor grayColor].CGColor;
myView.layer.shadowOffset = CGSizeMake(5, 5);
myView.layer.shadowOpacity = 1;
myView.layer.shadowRadius = 1.0;
Change
myView.layer.shadowOffset = CGSizeMake(5, 5);
to
myView.layer.shadowOffset = CGSizeMake(0, 5);
see if that works for you.
Related
i need right left and bottom shadow on UIImageView not below tried below code for shadow but not working as expected
viewCheck.layer.shadowRadius = 1.5f;
viewCheck.layer.shadowColor = [UIColor colorWithRed:176.f/255.f green:199.f/255.f blue:226.f/255.f alpha:1.f].CGColor;
viewCheck.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
viewCheck.layer.shadowOpacity = 0.9f;
viewCheck.layer.masksToBounds = NO;
UIEdgeInsets shadowInsets = UIEdgeInsetsMake(0, 0, -1.5f, 0);
UIBezierPath *shadowPath = [UIBezierPath
bezierPathWithRect:UIEdgeInsetsInsetRect(viewCheck.bounds, shadowInsets)];
viewCheck.layer.shadowPath = shadowPath.CGPath;
I am creating UITextField programatically and i am trying to create a textField that has only bottom border as given in the figure. Please help this problem in objective-c programatically ?
First:
Add and import QuartzCore framework.
#import <QuartzCore/QuartzCore.h>
Second:
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
EDIT:
If you have more TextFields, make one common method which takes UITextField and applies border to it like below:
-(void)SetTextFieldBorder :(UITextField *)textField{
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor grayColor].CGColor;
border.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField.frame.size.width, textField.frame.size.height);
border.borderWidth = borderWidth;
[textField.layer addSublayer:border];
textField.layer.masksToBounds = YES;
}
Pass your TextField as follows to set Bottom Border:
[self SetTextFieldBorder:YourTextField];
Or you can add a thin line subview to the textfield that will mimic the border:
UIView *lineView = [[UIView alloc] init];
lineView.translatesAutoresizingMaskIntoConstraints = false;
lineView.backgroundColor = [UIColor grayColor];
[textField addSubview:lineView];
[lineView.heightAnchor constraintEqualToConstant:1];
[lineView.leftAnchor constraintEqualToAnchor:textField.leftAnchor constant:5.0];
[lineView.rightAnchor constraintEqualToAnchor:textField.rightAnchor constant:-5.0];
[lineView.bottomAnchor constraintEqualToAnchor:textField.bottomAnchor constant:0.0];
Swift version:
let lineView = UIView()
lineView.translatesAutoresizingMaskIntoConstraints = false
lineView.backgroundColor = UIColor.grayColor()
textField.addSubview(lineView)
lineView.heightAnchor.constraintEqualToConstant(1)
lineView.leftAnchor.constraintEqualToAnchor(textField.leftAnchor)
lineView.rightAnchor.constraintEqualToAnchor(textField.rightAnchor)
lineView.bottomAnchor.constraintEqualToAnchor(textField.bottomAnchor)
Following some instructions at my first post, what I am doing is
1. create a class ( subclass of uiview)
2. fill it with gradient
3. add a uilabel on it
What I am doing is :
1. in xib, i drag and drop uiview onto the screen
2. change to 'customViewBackGround' in 'custom class'
and customViewbackGround looks like below :
-(void) layoutSubviews {
CGFloat height = 20.0;
CGFloat x = 5;
CGFloat y = 3;
CGRect rect = CGRectMake(x, y, self.bounds.size.width - 2 * x, height);
titleLabel.frame = rect;
titleLabel = [[UILabel alloc] init] ;
titleLabel.textAlignment = NSTextAlignmentLeft;
titleLabel.opaque = NO;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont systemFontOfSize:15];
titleLabel.textColor = [UIColor blackColor];
[self addSubview:titleLabel];
}
- (void)drawRect:(CGRect)rect {
titleLabel.text = #"How to redeem your e-gift card online";
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0
green:230.0/255.0
blue:230.0/255.0
alpha:1.0].CGColor;
CGColorRef separatorColor = [UIColor colorWithRed:208.0/255.0 green:208.0/255.0 blue:208.0/255.0 alpha:1.0].CGColor;
CGRect paperRect = self.bounds;
// Fill with gradient
drawLinearGradient(context, paperRect, whiteColor, lightGrayColor);
// Add white 1 px stroke
CGRect strokeRect = paperRect;
strokeRect.size.height -= 1;
strokeRect = rectFor1PxStroke(strokeRect);
CGContextSetStrokeColorWithColor(context, whiteColor);
CGContextSetLineWidth(context, 1.0);
CGContextStrokeRect(context, strokeRect);
// Add separator
CGPoint startPoint = CGPointMake(paperRect.origin.x, paperRect.origin.y + paperRect.size.height - 1);
CGPoint endPoint = CGPointMake(paperRect.origin.x + paperRect.size.width - 1, paperRect.origin.y + paperRect.size.height - 1);
draw1PxStroke(context, startPoint, endPoint, separatorColor);
}
However, the text of uilabel is not showing up after all.
Any ideas about this issue.
You can't set the frame of titleLabel before it's been initialized. Swap those two lines:
titleLabel = [[UILabel alloc] init] ;
titleLabel.frame = rect;
In layoutSubviews you are setting the frame on titleLabel before it is created. Set it after creating the label.
May i know how do i add bottom border to UILable only with shadow?
No top, left, right border.
this is not working. Here is the code I've tried but it's not working.
CALayer* layer = [titleLabel layer];
layer.frame = CGRectMake(-1, -1, titleLabel.frame.size.width, 1.0f);
layer.borderWidth=1;
[layer setBorderWidth:2.0f];
[layer setBorderColor:[UIColor blackColor].CGColor];
[layer setShadowOffset:CGSizeMake(-3.0, 3.0)];
[layer setShadowRadius:5.0];
[layer setShadowOpacity:5.0];
Test in this way:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
[lbl setText:#"Testo di prova..."];
[lbl setBackgroundColor:[UIColor clearColor]];
[[self view] addSubview:lbl];
[lbl sizeToFit];
CALayer* layer = [lbl layer];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor darkGrayColor].CGColor;
bottomBorder.borderWidth = 1;
bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1, layer.frame.size.width, 1);
[bottomBorder setBorderColor:[UIColor blackColor].CGColor];
[layer addSublayer:bottomBorder];
I hope this helps you
Just posting this answers for reference, since the user already got the answer,
Try this code in Swift and let me know how it goes..
func buttomBorder(label: UILabel) -> UILabel {
// For Buttom Border
let frame = label.frame
let bottomLayer = CALayer()
bottomLayer.frame = CGRect(x: 0, y: frame.height - 1, width: frame.width - 2, height: 1)
bottomLayer.backgroundColor = UIColor.lightGray.cgColor
label.layer.addSublayer(bottomLayer)
//For Shadow
label.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).CGColor
label.layer.shadowOffset = CGSizeMake(0.0, 2.0)
label.layer.shadowOpacity = 1.0
label.layer.shadowRadius = 0.0
label.layer.masksToBounds = false
label.layer.cornerRadius = 4.0
return label
}
Function Call:
labelName = buttomBoder(label: labelName)
I am trying to display a UIView with round corners and with a drop shadow. But the problem is that maskToBounds property only works for either of the case.
If maskToBounds is YES then round corners show up and when NO then shadow shows up. Here is the implementation but it only displays round corners with no shadow:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_gradient.jpeg"]]];
self.view.layer.masksToBounds = YES;
self.view.layer.opaque = NO;
self.view.layer.cornerRadius = 15.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
self.view.layer.shadowRadius = 5.0;
self.view.layer.shadowOffset = CGSizeMake(3.0, 3.0);
self.view.layer.shadowOpacity = 0.9f;
ideas!
NOTE: I have read and implemented the code in the following thread but it does not work:
UIView with rounded corners and drop shadow?
UPDATE 1:
I tried to create two separate views. One will represent the radius and one will represent the shadow. The problem is that is creates the shadow on top of the radius view as shown in the screenshot below:
H
ere is the code:
self.view.layer.masksToBounds = YES;
self.view.layer.opaque = NO;
self.view.layer.cornerRadius = 15.0f;
// self.view.backgroundColor = [UIColor clearColor];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_gradient.jpeg"]];
//
UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
shadowView.layer.shadowRadius = 2.0;
shadowView.backgroundColor = [UIColor clearColor];
shadowView.layer.shadowOffset = CGSizeMake(3.0, 3.0);
shadowView.layer.shadowOpacity = 0.9f;
shadowView.layer.shadowPath = [UIBezierPath
bezierPathWithRect:CGRectMake(0, 0, 100, 100)].CGPath;
[self.view addSubview:shadowView];
UPDATE 2:
Inverted still does not work. No round corners are created.
UIView *roundCornerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
roundCornerView.layer.masksToBounds = YES;
roundCornerView.layer.opaque = NO;
roundCornerView.layer.cornerRadius = 15.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
self.view.layer.shadowRadius = 2.0;
//self.view.backgroundColor = [UIColor clearColor];
self.view.layer.shadowOffset = CGSizeMake(3.0, 3.0);
self.view.layer.shadowOpacity = 0.9f;
self.view.layer.shadowPath = [UIBezierPath
bezierPathWithRect:CGRectMake(0, 0, 100, 100)].CGPath;
[self.view addSubview:roundCornerView];
SOLUTION:
UIView *roundCornerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
roundCornerView.layer.masksToBounds = YES;
roundCornerView.layer.opaque = NO;
roundCornerView.layer.cornerRadius = 15.0f;
roundCornerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_gradient.jpeg"]];
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
self.view.layer.shadowRadius = 2.0;
self.view.backgroundColor = [UIColor clearColor];
self.view.layer.shadowOffset = CGSizeMake(3.0, 3.0);
self.view.layer.shadowOpacity = 0.9f;
//self.view.layer.shadowPath = [UIBezierPath
// bezierPathWithRect:CGRectMake(0, 0, 100, 100)].CGPath;
[self.view addSubview:roundCornerView];
Create two views.
one with the drop shadow (and don't forget to set the shadowPath)
in which you add a subview with cornerRadius and maskToBounds.
The accepted answer didn't include any code, so here is an example in Swift (See the original question for the OP's solution in Obj-C).
Like the accepted answer, this solution uses separate views for the shadow and the corner radius.
// add the shadow to the base view
baseView.backgroundColor = UIColor.clear
baseView.layer.shadowColor = UIColor.black.cgColor
baseView.layer.shadowOffset = CGSize(width: 3, height: 3)
baseView.layer.shadowOpacity = 0.7
baseView.layer.shadowRadius = 4.0
// improve performance
baseView.layer.shadowPath = UIBezierPath(roundedRect: baseView.bounds, cornerRadius: 10).cgPath
baseView.layer.shouldRasterize = true
baseView.layer.rasterizationScale = UIScreen.main.scale
// add the border to subview
let borderView = UIView()
borderView.frame = baseView.bounds
borderView.layer.cornerRadius = 10
borderView.layer.borderColor = UIColor.black.cgColor
borderView.layer.borderWidth = 1.0
borderView.layer.masksToBounds = true
baseView.addSubview(borderView)
// add any other subcontent that you want clipped
let otherSubContent = UIImageView()
otherSubContent.image = UIImage(named: "lion")
otherSubContent.frame = borderView.bounds
borderView.addSubview(otherSubContent)
My full answer is here.
You can do it with a single view by applying following:
1. Firstly add a corner radius
yourview.layer.cornerRadius = 5.0
2. Call a function below
shadowToView(view : yourview)
func shadowToView(view : UIView){
view.layer.shadowOffset = CGSize(width: 0, height: 3)
view.layer.shadowOpacity = 0.6
view.layer.shadowRadius = 3.0
view.layer.shadowColor = UIColor.darkGray.cgColor
}