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;
}
Related
Right now I have this code, and I want to be able to change the color gradually as the angle of an arc changes.
I want to gradually change from green, to yellow, then orange, then red. But gradually, not just at once.
Any ideas on how to do this?
UIColor *greenColor = [UIColor greenColor];
greenColor = [UIColor colorWithRed:.0 green:1 blue:.0 alpha:0.5];
UIColor *grayColor = [UIColor grayColor];
grayColor = [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:0.5];
UIColor *redColor = [UIColor redColor];
redColor = [UIColor colorWithRed:1.0 green:.0 blue:.0 alpha:0.5];
UIColor *yellowColor = [UIColor yellowColor];
yellowColor = [UIColor colorWithRed:1.0 green:1.0 blue:.0 alpha:0.5];
UIColor *orangeColor = [UIColor orangeColor];
orangeColor = [UIColor colorWithRed:1.0 green:0.647 blue:.0 alpha:0.5];
int direction = 0;
if(index == 3){
direction = 1;
CGFloat currentEndAngle = atan2f(d_y, d_x);
CGFloat actualEndAngle = -0.49234065359; //was M_PI
if(fabs(currentEndAngle*180/M_PI) <= 91){
// Draw the optimal arc
//[self drawArc:ourPoint.startPosition andEndAngle:actualEndAngle andRadius:radius andColor:grayColor andDirection:direction withContext:context];
if(currentEndAngle > 0 || currentEndAngle > actualEndAngle){
// Draw the current green arc
if(currentEndAngle - actualEndAngle < 0.6){
[self drawArc:ourPoint.startPosition andEndAngle:currentEndAngle andRadius:radius andColor:orangeColor andDirection:direction withContext:context];
}
else{
[self drawArc:ourPoint.startPosition andEndAngle:currentEndAngle andRadius:radius andColor:greenColor andDirection:direction withContext:context];
}
}
else{
// Draw the current red arc
[self drawArc:ourPoint.startPosition andEndAngle:currentEndAngle andRadius:radius andColor:redColor andDirection:direction withContext:context];
missAngle += fabs(currentEndAngle-actualEndAngle);
}
}
As Ckouta says in his answer, what you want is a gradient. I don't think his code would do exactly what you want to do (drawing an arc with a color gradient) but it's a step in the right direction.
Getting an arc to draw using a gradient would be tricky. You'd either need to create custom Core Graphics code to render this yourself, use some combination of CALayers, layer masks, and CAGradientLayers, or maybe use Core Image filters to apply a radial gradient to an arc image.
i'm trying to make a UIImageView with rounder corner and white border, i have subclassed a UIImageView, this is the code:
MyUIImageView.h
#interface MyUIImageView : UIImageView
#end
MyUIImageView.m
#implementation MyUIImageView
-(void)layoutSubviews
{
self.layer.cornerRadius = CGRectGetWidth(self.frame)/2.f;
self.layer.borderColor = [[UIColor whiteColor] CGColor];
self.layer.borderWidth = kLineWidth;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
self.contentMode = UIViewContentModeScaleAspectFill;
self.backgroundColor = [UIColor colorWithRed:0.82 green:0.82 blue:0.83 alpha:1];
}
#end
this is the result:
seems fine, but there is a problem as you can see from here:
the image pops out from the borders edge, how i can avoid this problem? how i can cut the image exactly at the edge of the border?
Perhaps a better solution doesn't involve making another View at all - with two views you greatly increase complexity for animation etc, not to mention overhead to keep track of and manipulate both.
I'd instead create a shape layer and add it as a sublayer. Something like this:
CAShapeLayer border = [[CAShapeLayer alloc] init];
border.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds cornerRadius: self.layer.cornerRadius];
border.strokeColor = [UIColor whiteColor];
border.fillColor = [UIColor clearColor];
self.layer.addSublayer(border)
The benefit of doing it this way is that you can add it as a method on your UIImageView subclass if you wish. You can add a border to an object and forget about it, as long as you're not changing the frame of the base object. Transforms etc affect sublayers so you can scale, rotate, etc and not have gross edges.
Hope this helps!
Create a custom border like this:
UIImage *image = [UIImage imageNamed:#"spongebob.jpg"];
UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 200, 200)];
[borderView.layer setMasksToBounds:YES];
[borderView.layer setCornerRadius:borderView.frame.size.width/2.0f];
[borderView setBackgroundColor:[UIColor whiteColor]];
int borderWidth = 3.0f;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(borderWidth, borderWidth, borderView.frame.size.width-borderWidth*2, borderView.frame.size.height-borderWidth*2)];
[imageView.layer setCornerRadius:imageView.frame.size.width/2.0f];
[imageView.layer setMasksToBounds:YES];
[imageView setImage:image];
[borderView addSubview:imageView];
[self.view addSubview:borderView];
Now you image does not pop out of the border.
Hope this helps :)
How to drop a shadow on UIImageView which has a masked image?
I don't mean a rectangular shadow - I'd like to apply the same mask effect to shadow too.
To give the Shadow Effect to UIImageView Try below code..
1) #import <QuartzCore/QuartzCore.h> in .h file
2) To give a shadow effect to Cell's UIImageView
mediaImage.layer.shadowColor = [UIColor blackColor].CGColor;
mediaImage.layer.shadowRadius = 10.f;
mediaImage.layer.shadowOffset = CGSizeMake(0.f, 5.f);
mediaImage.layer.shadowOpacity = 1.f;
mediaImage.clipsToBounds = NO;
Well! You can try this one.
// Use a White background to make the shadow prominent.
self.view.backgroundColor = [UIColor whiteColor];
// The image we're going to mask and shadow
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.jpeg"]];
image.center = self.view.center;
// Make new layer to contain shadow and masked image
CALayer *containerLayer = [CALayer layer];
containerLayer.shadowColor = [UIColor blackColor].CGColor;
containerLayer.shadowRadius = 10.f;
containerLayer.shadowOffset = CGSizeMake(0.f, 5.f);
containerLayer.shadowOpacity = 1.f;
// Use the image's layer to mask the image into a circle
image.layer.cornerRadius = roundf(image.frame.size.width/2.0);
image.layer.masksToBounds = YES;
// Add masked image layer into container layer so that it's shadowed
[containerLayer addSublayer:image.layer];
// Add container including masked image and shadow into view
[self.view.layer addSublayer:containerLayer];
You can custom a subclass called NEWImageVIew inherit UIImageView.In NEWImageVIew you can make a property called realImageContainer,which you can set image to this property.
#interface NEWImageView : UIImageView
#property (nonatomic,strong) UIImageView *realImageContainer;
#end
#implementation NEWImageView
- (UIImageView *)realImageContainer {
if (!_realImageContainer) {
_realImageContainer = [UIImageView new];
[self addSubview:_realImageContainer];
[_realImageContainer mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
}
return _realImageContainer;
}
#end
When you want to make the image masked and shadowed, you can set image in realImageContainer and set mask to realImageContainer layer,so you get the masked layer.
NEWImageView *newImgView = [[NEWImageView alloc] init];
newImgView.backgroundColor = [UIColor clearColor];
newImgView.contentMode = UIViewContentModeScaleAspectFit;
newImgView.realImageContainer.image = self.image;
newImgView.realImageContainer.layer.mask = self.maskLayer;//this masklayer you can make youself
What about shadow?Because the realImageContainer is added to NEWImageView's view,the NEWImageView's layer not used yet,you can set shadow here.
newImgView.layer.shadowColor = [UIColor blackColor].CGColor;
newImgView.layer.shadowOpacity = 0.33;
newImgView.layer.shadowRadius = 8;
newImgView.layer.shadowOffset = CGSizeMake(0, 19);
so you get a masked and shadowed image.(I use a triangle image as mask,so I get this image)
you have to set an image with a transparent background and then you add the shadow like that:
imageView.layer.shadowColor = [UIColor blackColor].CGColor;
imageView.layer.shadowOpacity = 0.1;
imageView.layer.shadowRadius = 5;
imageView.layer.shadowOffset = CGSizeMake(5, 5);
[imageView setClipsToBounds:NO];
remember you have to import QuarzCore library
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:-
I just want to draw circle with stroke, the code below draws well but it fills the circle.
I do not want it filled. Please help me
self.circleView = [[UIView alloc] initWithFrame:CGRectMake(10,20,20,20)];
circleView.alpha = 0.5;
self.circleView.layer.cornerRadius = 50;
self.circleView.backgroundColor = [UIColor whiteColor];
change
self.circleView.backgroundColor = [UIColor whiteColor];
to
self.circleView.backgroundColor = [UIColor clearColor];
and for the border (Stroke) add
self.circleView.layer.borderColor = [[UIColor redColor] CGColor];
self.circleView.layer.borderWidth = 1;