Mohith P
Mohith P

Reputation: 585

Custom button in Objective C

I want to design a custom button in Objective C and the button is like a tick mark given bellow . Is it possible to design without using images?

enter image description here

Upvotes: 1

Views: 1211

Answers (2)

Bhargav Soni
Bhargav Soni

Reputation: 1077

Apply CAShapeLayer to your UIButton's Layer

Here is an example for you:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 90)];
btn.backgroundColor = [UIColor purpleColor];
UIBezierPath *bPath = [UIBezierPath bezierPath];
[bPath moveToPoint:CGPointMake(25, 40)];
[bPath addLineToPoint:CGPointMake(35, 60)];
[bPath addLineToPoint:CGPointMake(75, 20)];
CAShapeLayer *sLayer = [CAShapeLayer layer];
sLayer.strokeColor = [UIColor greenColor].CGColor;
sLayer.lineWidth = 3.0f;
sLayer.path = bPath.CGPath;
sLayer.fillColor = [UIColor clearColor].CGColor;
[btn.layer addSublayer:sLayer];
[self.view addSubview:btn];

Output:

Here is the output

Hope this helps!

Upvotes: 2

Sneha
Sneha

Reputation: 880

Use UIBezierPath & CAShapeLayer as follow..

UIBezierPath* mybezierpath = [[UIBezierPath alloc]init];
[mybezierpath moveToPoint:CGPointMake(40.0, 55.0)];
[mybezierpath addLineToPoint:CGPointMake(130.0,160.0)];
[mybezierpath addLineToPoint:CGPointMake(200.0, 60.0)];

CAShapeLayer *lines = [CAShapeLayer layer];
lines.path = mybezierpath.CGPath;
lines.bounds = CGPathGetBoundingBox(lines.path);
lines.strokeColor = [UIColor greenColor].CGColor;
lines.fillColor = [UIColor clearColor].CGColor; /*if you just want lines*/
lines.lineWidth = 3;
lines.position = CGPointMake(self.myButton.frame.size.width/2.0, self.myButton.frame.size.height/2.0);
lines.anchorPoint = CGPointMake(.5, .5);

[self.myButton.layer addSublayer:lines];

This will Give you result like this..

enter image description here

Adjust points of path According to the Button you have...

Hope this helps.

Upvotes: 2

Related Questions