如何处理 CATextLayer 以在 UILabel 中显示与系统字体相同的字体?

What to do with CATextLayer to have the same font displayed as the system font in UILabel?

当我使用UILabel时,我将系统字体设置为:

label.font = UIFont.systemFontOfSize(18, weight: UIFontWeightMedium)

现在尝试用CATextLayer上网查了一下,发现iOS9的系统字体是San Francisco,字体名称是.SFUIText-Medium,所以我尝试将字体名称设置为:

textLayer = CATextLayer() 
textLayer.font = CTFontCreateWithName(".SFUIText-Medium", 18, nil)

但是,如上所述,UILabelCATextLayer 设备屏幕上显示的字体不同。 CATextLayer 中使用的正确字体名称是什么,以便我可以显示与 UILabel 完全相同的字体?

自 iOS 7.0 起,UIFontCTFont 已“免费桥接”。这意味着您可以将 UIFont 视为 CTFont,反之亦然。因此:

let view = UIView(frame: CGRectMake(0, 0, 200, 100))
view.backgroundColor = UIColor.whiteColor()

let layer = CATextLayer()
layer.font = UIFont.systemFontOfSize(18, weight: UIFontWeightMedium)
layer.string = "Hello"
layer.frame = view.bounds
layer.foregroundColor = UIColor.blackColor().CGColor
view.layer.addSublayer(layer)

XCPlaygroundPage.currentPage.liveView = view
print(layer.font)

结果:

Optional(<UICTFont: 0x7f904bf38960> font-family: ".SFUIText-Medium"; font-weight: normal; font-style: normal; font-size: 18.00pt)