Swift: Uibezierpath For Inscribed In Addition To Circumscribed Circles (Xcode 7.2.1)

UIBezierPath has an inbuilt initialiser that returns an oval inward a rect together with of course of teaching nosotros tin move this to line circles yesteryear passing a square. However, assume yous convey a rectangular icon together with yous desire to mask it alongside a circle. For this you'll demand the inscribed circle non an oval. The minute scenario I desire to imagine is a circumscribed circle exterior the rect for where nosotros want, for example, text to period of time around a foursquare icon inward a UITextView. In gild to address this I started to intend virtually how nosotros line around an icon square. H5N1 foursquare text period of time is elementary to orbit alongside a rect larger than that of the image. Circular together with oval wraps pose a bigger problem:
But alongside a piffling assistance from the Internet inward finding the correct equations all these things tin endure achieved:

extension CGFloat {     func radians() -> CGFloat {         allow b = CGFloat(M_PI) * (self/180)         provide b     } }  extension UIBezierPath {          convenience init(circumscribedCircleRect rect:CGRect) {         allow halfWidth = rect.width / ii         allow halfHeight = rect.height / ii         allow radius = sqrt(halfWidth * halfWidth + halfHeight * halfHeight)         allow catch = CGPointMake(rect.midX, rect.midY)         self.init(arcCenter: center, radius: radius, startAngle: CGFloat(0).radians(), endAngle: CGFloat(360).radians(), clockwise: true)     }           convenience init(inscribedCircleRect rect:CGRect) {         allow halfWidth = rect.width / ii         allow halfHeight = rect.height / ii         allow radius = halfWidth >= halfHeight ? halfHeight : halfWidth         allow catch = CGPointMake(rect.midX, rect.midY)         self.init(arcCenter: center, radius: radius, startAngle: CGFloat(0).radians(), endAngle: CGFloat(360).radians(), clockwise: true)     }       } 
Now of course of teaching nosotros mightiness too desire an oval exterior a rect for flowing text every bit well. This is too possible to calculate:
convenience init(ovalOutsideRect rect:CGRect) {         allow width = rect.width         allow acme = rect.height         allow rootage = CGPointMake(rect.minX, rect.minY)         allow newSize = CGSizeMake(CGFloat(M_SQRT2) * width, CGFloat(M_SQRT2) * height)         allow newRect = CGRect(x: origin.x + (width - newSize.width) / 2, y: origin.y + (height - newSize.height) / 2, width: newSize.width, height: newSize.height)         self.init(ovalInRect:newRect)     }
But I've institute that inward existent move that UITextView doesn't piece of work good when an oval is unopen to the border of the page. Words halt upwards randomly broken together with characters are left trapped on the incorrect side of the curve. Here this is demonstrated alongside a circular icon (or rather a foursquare icon that was masked together with therefore a wind placed around it).
To solve this I've chosen to blueprint a path for when the icon is on the left side of the page together with would mirror this on the correct side. This represents the minimum wind required to a polish crease around the image:
The code for which reads:
convenience init(ovalOutsideRectTextFlowRight rect:CGRect) {         allow width = rect.width         allow acme = rect.height         allow rootage = CGPointMake(rect.minX, rect.minY)         allow newSize = CGSizeMake(CGFloat(M_SQRT2) * width, CGFloat(M_SQRT2) * height)         allow newRect = CGRect(x: origin.x + (width - newSize.width) / 2, y: origin.y + (height - newSize.height) / 2, width: newSize.width, height: newSize.height)         self.init(ovalInRect:newRect)         allow bezSquare = UIBezierPath(rect: CGRect(x: newRect.minX, y: newRect.minY, width: newRect.width / 2, height: newRect.height))         self.appendPath(bezSquare)     }

Applying this logic alongside the context of a text wind this is therefore achieved:

Conclusion

This post service has started to hold off at the usefulness of inscribed together with circumscribed circles. The move representative within a UITextView has been presented merely I've non stretched into discussing the code for this here. See intelligence of UITextView exclusionPaths inward this post for to a greater extent than exceptional on this expanse together with too the accompanying demo which includes an oval exclusion expanse that tin endure dragged together with volition present yous the exact occupation alongside the fragmentation of words.

Comments

Popular posts from this blog

Removing The Index.Php File From Url Inward Codeigniter

What Are The Main Components of a Computer System

Delete Daily Doppler E-Mail Spam From An Iphone [Fix]