I will try to reduce this to the minimum you need to get this to work. This code may differ significantly from what you need to write for Xcode 10 / Swift 4.2. Hopefully Xcode will suggest some good fixes.
This first section is going to try to figure out exactly how high your numbers are.
func drawHourNumbers (rect : CGRect) {
let fontSize = baseWidth * 14
let attributes : NSDictionary = [
NSForegroundColorAttributeName : color,
NSParagraphStyleAttributeName : NSMutableParagraphStyle(),
NSObliquenessAttributeName : 0,
NSFontAttributeName : UIFont(
name : "HoeflerText-Regular", size: 18)!
];
var maxHeight : CGFloat = 0
for i in 1...12 {
let s = "\(i)"
let h = s.size(attributes: attributes as? [String : Any]).height
if h > maxHeight {
maxHeight = h
}
}
This next section will draw the numbers taking into account the width and height of the string (
i.e. it is going to help you center them using an offset).
let attributes2 : NSDictionary = [
NSForegroundColorAttributeName : color,
NSParagraphStyleAttributeName : NSMutableParagraphStyle(),
NSObliquenessAttributeName : 0,
NSFontAttributeName : UIFont(
name : font,
size: fontSize * fontSize / maxHeight)!
];
for i in 1...12 {
let loc = angle(fromHour: i)
let s = "\(i)"
let w = s.size(attributes: attributes2 as? [String : Any]).width
let h = s.size(attributes: attributes2 as? [String : Any]).height
s.draw(with: CGRect(x: loc.x-w/2,
y: loc.y-(fontSize*0.55),
width: w,
height: h),
options: .usesLineFragmentOrigin,
attributes: attributes2 as? [String : Any], context: nil)
}
}