Rounder corners in UIView - objective-c

i have a UIView and i would like to round it and add shadow color like this image :

For Rounder Corner
CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerBottomLeft | UIRectCornerTopRight cornerRadii: (CGSize){10.0, 10.}].CGPath;
self.layer.mask = maskLayer;
For Shadow
self.layer.masksToBounds = NO;
self.layer.shadowOffset = CGSizeMake(-15, 20);
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = 0.5;
Hope this helps you!

To get the very same shadow as your image I would recommend you using a background image. Otherwise you should include <Quartzcore/Quartzcore.h> and use the following code:
view.layer.cornerRadius = 10;
view.frame = CGRectMake(15, 15, 100, 100);
view.backgroundColor = [UIColor redColor];
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(2.0, 2.0);
view.layer.shadowOpacity = 0.8;
view.layer.shadowRadius = 10;
Some more information for the background image-option
Create a UIView that has the same width and height as the image including the shadow and assign the image to it this way:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundview.png"]];

Related

Draw a border around an oval CAShapeLayer on MacOS

I need to display an oval layer with a predefined fillColor and a 1px borderColor.
Here is code:
CGRect r = CGRectMake(28, self.bounds.size.height - 5 - 12, 12.0, 12.0);
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:r];
NSColor *fillColor = [NSColor colorWithRed:255.0/255.0 green:193.0/255.0 blue:47.0/255.0 alpha:1.0f];
NSColor *borderColor = [NSColor colorWithRed:226.0/255.0 green:70.0/255.0 blue:84.0/255.0 alpha:1.0f];
minimizeButtonLayer = [CAShapeLayer layer];
minimizeButtonLayer.path = path.CGPath;
minimizeButtonLayer.fillColor = fillColor.CGColor;
minimizeButtonLayer.borderColor = borderColor.CGColor;
minimizeButtonLayer.borderWidth = 1.0;
The oval shape is correctly displayed but without any border.
Why? Please help me.
P.S. Please note that path.CGPath is from a NSBezierPath category.
I solved the problem by replacing borderColor with strokeColor
minimizeButtonLayer.borderColor = borderColor.CGColor;
with
minimizeButtonLayer.strokeColor = borderColor.CGColor;

How to create only bottom border for label?

Hi I used the the following code to create a bottom border to my label
CALayer *border = [CALayer layer];
CGFloat borderWidth = 2;
border.borderColor = [UIColor darkGrayColor].CGColor;
border.frame = CGRectMake(0, _label.frame.size.height - borderWidth, _label.frame.size.width, _label.frame.size.height);
border.borderWidth = borderWidth;
[_label.layer addSublayer:border];
_label.layer.masksToBounds = YES;
But it the border is breaks at half of label. How can I fix it. Thanks in advance.
You can do like this :
With the use of CALayer you can create border on UILabel or any other UIControl.
1) Bottom Border
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor blackColor].CGColor;
bottomBorder.borderWidth = 1;
bottomBorder.frame = CGRectMake(0, CGRectGetHeight(myLabel.frame)-1, CGRectGetWidth(myLabel.frame), 1);
myLabel.clipsToBounds = YES;
[myLabel.layer addSublayer:bottomBorder];
2) Top Border
CALayer *topBorder = [CALayer layer];
topBorder.borderColor = [UIColor blackColor].CGColor;
topBorder.borderWidth = 1;
topBorder.frame = CGRectMake(0, 0, CGRectGetWidth(myLabel.frame), 1);
myLabel.clipsToBounds = YES;
[myLabel.layer addSublayer:topBorder];
3) Left Border
CALayer *leftBorder = [CALayer layer];
leftBorder.borderColor = [UIColor blackColor].CGColor;
leftBorder.borderWidth = 1;
leftBorder.frame = CGRectMake(0, 0, 1, CGRectGetHeight(myLabel.frame));
myLabel.clipsToBounds = YES;
[myLabel.layer addSublayer:leftBorder];
4) Right Border
CALayer *rightBorder = [CALayer layer];
rightBorder.borderColor = [UIColor blackColor].CGColor;
rightBorder.borderWidth = 1;
rightBorder.frame = CGRectMake(CGRectGetWidth(myLabel.frame)-1, 0, 1, CGRectGetHeight(myLabel.frame));
myLabel.clipsToBounds = YES;
[myLabel.layer addSublayer:rightBorder];
Try it,
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];
You need to calculate width of label based on string and also font which you require.
You can get size like this
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
and use expectedLabelSize while setting frame for your border.
border.frame = CGRectMake(0, _label.frame.size.height - borderWidth, _expectedLabelSize.width , _label.frame.size.height);
Hope it helps. Happy Coding !!
Please try with the following code.
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];

can not add a shadow to the view, CALayer

I am adding a shadow to my view by following
- (void)viewDidLoad{
[super viewDidLoad];
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
self.view.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.view.layer.shadowOpacity = 1.0f;
self.view.layer.shadowRadius = 4.0f;
self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
}
However, I am getting a view with no shadow at all like below
Did I miss some points in the middle of the way. Please advice me on this issue
You need to set layer.masksToBounds to NO and clipsToBounds to YES.
self.view.layer.masksToBounds = NO;
self.view.clipsToBounds = YES;
You are not offsetting your shadow at all.
Try:
self.view.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
Sorry all, this is my stupid mistake. The view structure I am having is
view(UIView) ( in white color )
|
|
aView (UIView) ( in orange color )
what I did was to show the shadow of view not aView. Just corrected the code like below
(void)viewDidLoad{
[super viewDidLoad];
self.aView.layer.shadowColor = [UIColor blackColor].CGColor;
self.aView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.aView.layer.shadowOpacity = 1.0f;
self.aView.layer.shadowRadius = 4.0f;
self.aView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
}
making the offset 0, will produce a nil effect on the view.
you can try this:
-(void)makeShadow
{
self.layer.shadowOpacity = 0.7f;
self.layer.shadowOffset = CGSizeMake(5.0f,3.0f);
self.layer.shadowColor =[[UIColor blackColor] CGColor];
// UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
// layer.shadowPath = path.CGPath;
// layer.shadowRadius = 5.0f;
}
-(void)makeCornerRadius:(CGFloat)_cornerRadius
{
self.cornerRadius = _cornerRadius;
[self.layer setMasksToBounds:NO];
[self.layer setCornerRadius:_cornerRadius];
}
you should make the offset have a certain value not 0.
for example:
self.view.layer.shadowOffset = CGSizeMake(3, -1);
so that there will be a shadow forming in your view.
Make sure that self.clipsToBounds = NO;
Your view looks unrelated to the code you provided. Be sure you are applying the shadow to the correct view, you are not masking the view to its bounds, and the colors are correct. The code below shows a working example:
UIView *pointView = [[UIView alloc] initWithFrame:pointLabel.frame];
pointView.backgroundColor = [UIColor whiteColor];
pointView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
pointView.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
pointView.layer.shadowOpacity = 1.0f;
pointView.layer.shadowRadius = 5.0f;
pointView.layer.shadowPath = [UIBezierPath bezierPathWithRect:pointView.bounds].CGPath;

UITextView Shadow Not Working

I am trying to get a UITextView's layer's shadow to work in a UITableView header. I have a UIView that I am formatting everything in, and then setting the headerview equal to it.
UIView * view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tableView.frame.size.width, 450);
UIColor * baseColor = [[UIColor alloc] initWithRed: 45/255.0
green: 100/255.0
blue: 150/255.0
alpha: 1.0];
view.backgroundColor = baseColor;
...
UITextView * newCommentField = [[UITextView alloc] initWithFrame:CGRectMake(20, 230, 270, 120)];
newCommentField.text = #"New Comment";
newCommentField.tag = 3;
newCommentField.layer.shadowOffset = CGSizeMake(0, 3);
newCommentField.layer.shadowRadius = 5.0;
newCommentField.layer.shadowColor = [UIColor blackColor].CGColor;
newCommentField.layer.shadowOpacity = 0.8;
[view addSubview:newCommentField];
...
self.tableView.tableHeaderView = view;
Everything shows up properly in the view. However, the shadow is not appearing. I don't know what is going wrong here. I have even tried modifying the layer's frame and making it the size of the comment field, bigger and the same size.
You're not setting background color for newCommentField.
Try:
newCommentField.backgroundColor = [UIColor whiteColor];
You can do it this way also... Create a view (eg. bgView) of same size as your textView and add that textView as a subView of bgView, which will be added as subView of you header view. Apply the shadow effect to the bgView instead.
UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(20, 230, 270, 120)];
bgView.backgroundColor=[UIColor whiteColor];
bgView.layer.shadowOffset = CGSizeMake(0, 3);
bgView.layer.shadowRadius = 5.0;
bgView.layer.shadowColor = [UIColor blackColor].CGColor;
bgView.layer.shadowOpacity = 0.8;
[bgView addSubview:newCommentField];
[view addSubview:bgView];

Using a CALayer as a mask to a backing layer of a GLKView blocks touch events?

I'm trying to use a CAShapeLayer as a mask to the backing layer of a GLKView like so:
CGRect circleFrame = CGRectMake(0, 0, 800, 800);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:circleFrame];
CGPoint circleAnchor = CGPointMake(CGRectGetMidX(circleFrame) / CGRectGetMaxX(circleFrame),CGRectGetMidY(circleFrame) / CGRectGetMaxY(circleFrame));
shapeLayerMask = [[CAShapeLayer alloc] init];
shapeLayerMask.path = path.CGPath;
shapeLayerMask.anchorPoint = circleAnchor;
shapeLayerMask.frame = CGRectMake(0, 0,800, 800);
shapeLayerMask.fillColor = [UIColor blackColor].CGColor;
shapeLayerMask.backgroundColor = [UIColor clearColor].CGColor;
shapeLayerMask.opacity = 1.0f;
shapeLayerMask.hidden = NO;
self.view.layer.mask = shapeLayerMask;
But as soon as i do this, all my touch events stop firing. No more touchesBegan, touchesEnded, etc. Anyone knows why? Thanks in advance!