With the help of iOSKit I was able to convert some code found on GitHub to Xojo code:
Code:
[code] //Source: http://stackoverflow.com/questions/26129428/badge-view-in-uitableviewcell
// Count > 0, show count
If (forCount > 0) Then
// Create label
Dim fontSize As Single = 14
Dim lbl As New iOSLabel
Dim Font As iOSFont = iOSFont.SystemFont(fontSize)
lbl.TextFont = Font
lbl.TextAlignment = iOSTextAlignment.Center
lbl.TextColor = &cFFFFFF
Dim p As New iOSBitmap(1, 1, 2)
Dim g As iOSGraphics = p.graphics
g.TextFont = Font
Declare Sub setBackgroundColor Lib UIKitLib selector "setBackgroundColor:" (obj_id As ptr, col As ptr)
setBackgroundColor(lbl.Handle, New UIColor(&cFF0000))
// Add count to label and size to fit
lbl.Text = forCount.ToText
Declare Sub setAdjustsFontSizeToFitWidth Lib UIKitLib selector "setAdjustsFontSizeToFitWidth:" (id As ptr, value As Boolean)
setAdjustsFontSizeToFitWidth lbl.Handle, True
// Adjust frame to be square for single digits or elliptical for numbers > 9
#If Target32Bit
Declare Sub drawInRect Lib UIKitLib selector "drawInRect:" (obj_id As ptr, rect As CGRect32)
Dim frame As CGRect32
#ElseIf Target64Bit
Declare Sub drawInRect Lib UIKitLib selector "drawInRect:" (obj_id As ptr, rect As CGRect64)
Dim frame As CGRect64
#EndIf
#If Target32Bit
Declare Function frame_ Lib UIKitLib selector "frame" (view As ptr) As CGRect32
#ElseIf Target64Bit
Declare Function frame_ Lib UIKitLib selector "frame" (view As ptr) As CGRect64
#EndIf
frame = frame_(lbl.handle)
frame.Origin.x = 0
frame.origin.y = 0
frame.rsize.h = xojo.math.Round(Font.XHeight + Font.CapHeight*2)
frame.rsize.w = If(forCount <= 9, frame.rsize.h, g.TextLineSize(lbl.Text).Width + fontSize)
#If Target32Bit
Declare Sub setFrame Lib UIKitLib selector "setFrame:" (obj_id As ptr, frame As CGRect32)
setFrame(lbl.Handle, frame)
#ElseIf Target64Bit
Declare Sub setFrame Lib UIKitLib selector "setFrame:" (obj_id As ptr, frame As CGRect64)
setFrame(lbl.Handle, frame)
#EndIf
// Set radius and clip to bounds
Declare Function layer_ Lib UIKitLib selector "layer" (obj_id As ptr) As ptr
Declare Sub clipsToBounds Lib UIKitLib selector "setClipsToBounds:" (obj_id As ptr, value As Boolean)
#If Target32Bit
Declare Sub setCornerRadius_ Lib "QuartzCore.framework" selector "setCornerRadius:" (obj_id As ptr, value As Single)
#ElseIf Target64Bit
Declare Sub setCornerRadius_ Lib "QuartzCore.framework" selector "setCornerRadius:" (obj_id As ptr, value As Double)
#EndIf
Dim layer As ptr = layer_(lbl.Handle)
setCornerRadius_(layer, frame.rsize.h/2.0)
clipsToBounds(lbl.Handle, True)
//Show label in accessory view and remove disclosure
Declare Sub setAccessoryView Lib "UIKit" selector "setAccessoryView:" (obj_id As ptr, acc_id As ptr)
setAccessoryView(cell.Handle, lbl.Handle)
cell.AccessoryType = iOSTableCellData.AccessoryTypes.None
Else
cell.AccessoryType = iOSTableCellData.AccessoryTypes.Disclosure
End If[/code]
This code works perfectly as long as the Table has no CustomCells.
In the screenshot below, “Semester 1 | Semester 2” is a CustomCell. The cell badge for the Exceptions row is placed at 0:0 in the cell. As if the AccessoryView was smaller and placed at the top left corner.