Gradient Stroke on UIButton - iphone

I've found the GradientButton class for gradients on a UIButton - however I can't figure out how to get a matching stroke around the button. How do you create a gradient stroke on the button?

You need to use CALayer for this:
[[button layer] setCornerRadius:8.0f];
[[button layer] setMasksToBounds:YES];
[[button layer] setBorderWidth:1.0f];
And dont forget to import:
#import <QuartzCore/QuartzCore.h>

Here is a good tutorial of making tutorial with Core Animation by Matt Long
http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/

Related

set bordercolor for image which is in array

I have images in an array, and when I display those images on view, it should appear with the border. How do I set the border color?
for(UIImage *img in imagesArray)
{
//for bordercolor
}
First Add QuartzCore.framework framework in your project from Build Phases => Link Binary with Libraries => Add button(+) and after import its file in your class like bellow...
#import <QuartzCore/QuartzCore.h>
and use bellow code for set color..
yourImageView.layer.borderWidth = 2.0;
yourImageView.layer.borderColor= [UIColor redColor].CGColor;
UPDATE:
-(IBAction)lockword:(id)sender {
for (UIImage *img3 in imagesArray) {
UIImageView *imgview1=[[UIImageView alloc]initWithImage:img3];
imgview1.layer.borderWidth = 2.0;
imgview1.layer.borderColor = [UIColor redColor].CGColor;
[imgview1 release];
// your other code write here
// Add this UIImageView as a subview of your view with its frame...
}
}
#import <QuartzCore/QuartzCore.h>
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
To allow editing the border color property of your imageview, you need to add Quartzcore framework
Add it from : Link Binary with Libraries option and then write the following code where you want to change the border color :
[imageview.layer setBorderWidth:2.0]; // set the border width to any value you want so that your bordercolor would appear accordingly
[imageview.layer setBorderColor:[[UIColor blueColor].CGColor]; // set the color to whatever background color you want to set to your image

How can I add a picture frame to an UIImage?

I am working on an iphone application where I want to add a different frame(e.g. black grit, oldtime, etc) to an image and save that image with that frame.
Is it possible in iphone application. Please give me suggestion.Thanks.
Check this question.
You need to implement following code:
#import <QuartzCore/QuartzCore.h>
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];

CALayer Border is appearing above subview (Z-order related, I think)

I have searched but could not find the reason for this behavior.
I have a UIButton whose image I am setting. Here is how the button should appear. Note that this is just a photoshop of the intended button design:
Essentially, it is a square custom UIButton with a white border and a little surrounding shadow. In the upper right corner, there is a "X" mark, that will be added programmatically as a subview.
Here is the screenshot of the button within the actual app. At this point, I have only added a shadow and the X mark as a subview:
How, when I try to add the white border, here is what it looks like:
It seems that the white border is appearing above the X mark sublayer. I don't know why.
Here is the code that I am using:
// selectedPhotoButton is the UIButton with UIImage set earlier
// At this point, I am adding in the shadow
[selectedPhotoButton layer] setShadowColor:[[UIColor lightGrayColor] CGColor]];
[[selectedPhotoButton layer] setShadowOffset: CGSizeMake(1.0f, 1.0f)];
[[selectedPhotoButton layer] setShadowRadius:0.5f];
[[selectedPhotoButton layer] setShadowOpacity:1.0f];
// Now add the white border
[[selectedPhotoButton layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[[selectedPhotoButton layer] setBorderWidth:2.0];
// Now add the X mark subview
UIImage *deleteImage = [UIImage imageNamed:#"nocheck_photo.png"];
UIImageView *deleteMark = [[UIImageView alloc] initWithFrame:CGRectMake(53, -5, 27, 27)];
deleteMark.contentMode = UIViewContentModeScaleAspectFit;
[deleteMark setImage:deleteImage];
[selectedPhotoButton addSubview:deleteMark];
[deleteMark release];
I don't understand why the border is appearing above the deleteMark subview. Is there any way to get the intended effect?
Thank you!
From Apple's docs on CALayer:
The border is drawn inset from the receiver’s bounds by borderWidth. It is composited above the receiver’s contents and sublayers and includes the effects of the cornerRadius property.
In order to get the effect you want, I suggest you put the image into an own subview/sublayer and set that sublayer's borderWidth property.
You can set the layer's zPosition to -1. That worked for me.
I had similar problem (I wanted to prevent border line to be on top of my subviews)
CAShapeLayer * _border = [CAShapeLayer layer];
_border.strokeColor = [UIColor colorWithRed:119/255.0f green:119/255.0f blue:119/255.0f alpha:1.0f].CGColor;
_border.fillColor = nil;
[bgRoundView.layer addSublayer:_border];
_border.path = [UIBezierPath bezierPathWithRoundedRect:bgRoundView.bounds cornerRadius:20.f].CGPath;

UIButton with Custom border colour, iPhone

I want to create Custom UIButton with rectangular shape. for this I am using rectangular view as a Background for UIButton and making UIbuttons background colour as clearcolor. But border of UIbutton still remains. any suggestions on how to make border disappear?
Thanks for any help in advance..
Try this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button.layer setBorderColor:[[UIColor clearColor] CGColor]];
but don't forget to add in your .h or .m file
#import <QuartzCore/QuartzCore.h>
I wasted a few minutes and got frustrated that the border was not showing up even though I was setting the layer property as suggested above.
When I set the width using the following, I saw the button border.
[[button layer] setBorderWidth:2.0f];
button.layer.borderColor = UIColor.orangeColor().CGColor
swift example for orange border
You can set the border properties on the CALayer by accessing the layer property of the button.
Add Quartz
#import <QuartzCore/QuartzCore.h>
Set properties of the button:
[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor AnyColor].CGColor];
You should set type of your UIButton to Custom, not Rounded Rect (if you are creating button via IB).
If you are creating UIButton programmatically then you this code:
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
And after that you can make your own customizations.

Rounded Rectangle Issue

I have seen many Q's on this subject but none are exactly what I am trying to do. I have a view inside of a view and it's frame is a CGRectangle. I would like said rectangle to have rounded edges to look like a rounded rectangle button. If I could have some code examples on how to implement that it would be nice. Thank you in advance.
You first need to import QuartzCore framework into your project. I hope you know how to do that. Than you can use the following code:
CALayer *l = [yourView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
// You can even add a border
[l setBorderWidth:1.0];
[l setBorderColor:[[UIColor blackColor] CGColor]];
Hope it helps! ;)
You need to make sure you import <QuartzCore/QuartzCore.h> and add QuartzCore to the Existing Frameworks in order to gain access to the cornerRadius: method.
Then to set the corner radius you would use something like the following depending upon your implementation of the view
UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(10,10,100,200)];
CALayer *theViewLayer = [theView layer];
[theViewLayer setCorderRadius:5.0];
//Other Methods you can use
[theViewLayer setBorderColor:[[UIColor colorWithWhite:1.0 alpha:0.3] CGColor]];
[theViewLayer setBorderWidth:2.0];
[theViewLayer setBackgroundColor:[[UIColor blackColor] CGColor]];