Rounded Corners

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!

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)

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

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)