I'm trying to use CoreText to create fonts with a specific font weight. I'm trying to use a CTFontDescriptor that I've created with an attributes dictionary specifying the font's name and font's weight. The correct font family is getting used, however, the weight doesn't seem to be getting set. Here's my code in Swift:
let fontDescriptorAttributes = [
kCTFontNameAttribute: "Courier",
kCTFontTraitsAttribute: [
kCTFontWeightTrait: NSFont.Weight.black
]
] as [CFString : Any]
let fontDescriptor = CTFontDescriptorCreateWithAttributes(fontDescriptorAttributes as CFDictionary)
var identityMat = CGAffineTransform(scaleX: 1.0, y: 1.0)
let font = CTFontCreateWithFontDescriptor(fontDescriptor, 24, &identityMat)
// Use the resulting font with a Core Animation text layer
let textLayer = CATextLayer()
textLayer.font = font
textLayer.string = "foo bar baz!"
textLayer.frame = CGRect(x: 0, y:0, width: textLayer.preferredFrameSize().width, height: textLayer.preferredFrameSize().height)
view.layer.addSublayer(textLayer) // add text layer to view's layer hierarchy
I've used the resulting font with both a CATextLayer
and CFAttributedString
but neither of the resulting graphics has the correct font weight. Should I be setting it differently?