# Rounded Corners

**URL:** <https://forum.xojo.com/t/rounded-corners/54587>\
**Category:** iOS\
**Created:** [July 6, 2020, 2:22pm UTC](https://forum.xojo.com/t/rounded-corners/54587 "2020-07-06T14:22:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoph\_Emrich](https://forum.xojo.com/user_avatar/forum.xojo.com/christoph_emrich/32/1727_2.png) [@Christoph\_Emrich](https://forum.xojo.com/u/Christoph_Emrich)\
**Post date:** [July 6, 2020, 2:22pm UTC](https://forum.xojo.com/t/rounded-corners/54587/1 "2020-07-06T14:22:08Z")

</div>

I’m pretty sure it’s been asked before but I haven’t found it in the forum: is there a way to have rounded corners for a rectangle, but only for the two top corners, i.e., the left top and the right top corner?

Thank you!

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [August 11, 2020, 1:06pm UTC](https://forum.xojo.com/t/rounded-corners/54587/2 "2020-08-11T13:06:31Z")

</div>

You could try using iOSPath like this in a Canvas.Paint event:

```
Const Pi = 3.14159
Const x = 50
Const y = 50
Const rectHeight = 200
Const rectWidth = 100
Const arcRadius = 20

Var topRoundRect As New iOSPath
topRoundRect.AddArc(x, y, arcRadius, Pi, 1.5 * Pi, False)
topRoundRect.AddArc(x + rectWidth, y, arcRadius, 1.5 * Pi, 0, False)
topRoundRect.LineToPoint(x + rectWidth + arcRadius, rectHeight)
topRoundRect.LineToPoint(x - arcRadius, rectHeight)
topRoundRect.LineToPoint(x - arcRadius, y)
g.DrawPath(topRoundRect)

```

 ![2020-08-11_09-05-04](https://forum.xojo.com/uploads/default/original/1X/617252450d153b18a9c5868db2d9ddeaccc28d43.png)

---

<div class="post-metadata">

**Author:** ![Jeremie\_L](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremie_l/32/150_2.png) [@Jeremie\_L](https://forum.xojo.com/u/Jeremie_L)\
**Post date:** [August 11, 2020, 2:31pm UTC](https://forum.xojo.com/t/rounded-corners/54587/3 "2020-08-11T14:31:18Z")

</div>

Sort of, but it doesn’t work if the rectangle has a border (border width \> 0)

```auto
Private Sub SetCorners()
  
  
  declare function NSClassFromString lib "Foundation" (clsName as CFStringRef) as ptr
  
  Declare Function layer_ Lib "UIKit.framework" selector "layer" (id As ptr) As Ptr
  Declare Function bezierPathWithRoundedRect Lib "UIKit.framework" selector "bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:" _
  (cls_ref as ptr, bounds as xcCGRect, corners as Integer, radius as xcCGSize) as ptr
  
  
  Declare Function bounds Lib "UIKit.framework" selector "bounds" (obj_id As Ptr) As xcCGRect
  Dim boundRect As xcCGRect = bounds(Rectangle1.handle)
  
  
  Const radius as Double = 8.0
  
  Dim corners As Integer = 3
  
  Dim radSize As xcCGSize
  radSize.width = radius
  radSize.height = radius
  
  
  Dim maskPath As Ptr
  maskPath = bezierPathWithRoundedRect(NSClassFromString("UIBezierPath"), _
  boundRect, corners, radSize)
  
  Dim maskLayer As Ptr
  maskLayer = layer_(NSClassFromString("CAShapeLayer"))
  
  
  
  Declare Sub setFrame_ Lib "UIKit.framework" selector "setFrame:" (obj_id As ptr, frame As xcCGRect)
  setFrame_(maskLayer, boundRect)
  
  Declare Sub setPath_ Lib "UIKit.framework" selector "setPath:" (obj_id As ptr, path As ptr)
  Declare function CGPath_ lib "UIKit.framework" selector "CGPath" (obj_id as ptr) as ptr
  setPath_(maskLayer, CGPath_(maskPath))
  
  
  Dim layer As ptr = layer_(Rectangle1.Handle)
  
  Declare Sub setMask Lib "UIKit.framework" selector "setMask:" (obj_id As ptr, mask As ptr)
  setMask(layer, maskLayer)
  
  Declare Sub masksToBounds_ Lib "UIKit.framework" selector "setMasksToBounds:" (id As ptr, value As Boolean)
  'masksToBounds_(layer, False)
End Sub

```

Code needs to be called after the View.Activate event, so either by pressing a button or using `xojo.core.timer.CallLater(100, AddressOf SetCorners)`

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [April 29, 2021, 4:29pm UTC](https://forum.xojo.com/t/rounded-corners/54587/4 "2021-04-29T16:29:23Z")

</div>


