Kashif Jilani
Kashif Jilani

Reputation: 1257

iOS: UIButton draw circle border in iOS

I need to draw a circle like border outside UIButton. Below is the attach image and code. I also need to show text below button. Following code will add image and text beneath that. But how do I have layer.enter image description here

   UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [imageButton setBackgroundImage:image forState:UIControlStateNormal];
    imageButton.translatesAutoresizingMaskIntoConstraints = NO;
    imageButton.backgroundColor = [UIColor clearColor];
    [imageButton setTitle:title forState:UIControlStateNormal];
    imageButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    imageButton.titleLabel.font = [UIFont fontWithName:@"OpenSans" size:fon];
    [imageButton setTitleEdgeInsets:UIEdgeInsetsMake(95, 0.0f, 0.0f, 0.0f)];
    [imageButton setTitleColor:[UIColor iconTextColor] forState:UIControlStateNormal];
    [imageButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

Upvotes: 1

Views: 1417

Answers (2)

techcraze
techcraze

Reputation: 112

You can do something like this.

borderView = UIView()

yourButton = UIButton()

borderView.frame.size.width = yourButton.frame.size.width + 1

borderView.layer.cornerRadius = borderView.frame.size.width/2

borderView.clipToBounds = true

borderView.backgroundColor = UIColor.clearColor()

borderView.layer.borderWidth = 1

borderView.layer.borderColor = UIColor.blueColor()

Upvotes: 0

caldera.sac
caldera.sac

Reputation: 5088

set the button width and height ex : width =100 and height = 100, should be same if you want a round then,

imageButton.layer.cornerRadius = 50 // this value should be half from width or height (width = height)
imageButton.layer.masksToBounds = YES

//if you want border with above

 imageButton.layer.borderWidth = 1;
 imageButton.layer.borderColor = [[UIColor blackColor] CGColor];

Upvotes: 2

Related Questions