Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawRect with rounded corners

I have this code for UIVIew:

override func drawRect(rect: CGRect) {

    let toolbarSize = CGFloat(UIDevice.currentDevice().userInterfaceIdiom == .Pad ? 0 : 54)

    let width = CGRectGetWidth(self.frame)
    let height = CGRectGetHeight(self.frame) - toolbarSize

    let heightSpan = floor(height / 2 - self.cropSize.height / 2)
    let widthSpan = floor(width / 2 - self.cropSize.width / 2)

    // fill outer rect
    UIColor(red: 0, green: 0, blue: 0, alpha: 0.5).set()
    UIRectFill(self.bounds)

    // fill inner border
    UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).set()
    UIRectFrame(CGRectMake(widthSpan - 2, heightSpan - 2, self.cropSize.width + 4,
        self.cropSize.height + 4))

    // fill inner rect
    UIColor.clearColor().set()
    UIRectFill(CGRectMake(widthSpan, heightSpan, self.cropSize.width, self.cropSize.height))
}

This draws a rect with a white border to my UIView, I want to add a corner radius to draw a circle.

Is it possible to do this?

like image 445
Luca Becchetti Avatar asked Jan 06 '23 18:01

Luca Becchetti


1 Answers

this works nice for me

    someImageView.layer.borderWidth = 1
    someImageView.layer.borderColor = UIColor.blackColor().CGColor
    someImageView.layer.cornerRadius = someImageView.frame.height/2
    someImageView.clipsToBounds = true
like image 162
DeyaEldeen Avatar answered Jan 15 '23 15:01

DeyaEldeen