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

时间:2016-02-18 02:44:42

标签: objective-c swift catextlayer

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

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

现在我尝试使用CATextLayer并在线搜索,发现iOS 9的系统字体为San Francisco,字体名称为.SFUIText-Medium,因此我尝试设置字体名称为:

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

但是,设备屏幕上显示的字体与UILabel& CATextLayer,如上所述。这里CATextLayer使用的字体名称是什么,这样我可以使用与UILabel完全相同的字体?

1 个答案:

答案 0 :(得分:6)

自iOS 7.0起,CTFontUIFont已被“免费桥接”。这意味着您可以将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)

结果:

screen shot

def exec(f: (a:Int, b:Int) => Boolean): Boolean = f(a,b)
              ^      ^
              |      |
//   These are supposed to be types, but a: Int and b: Int aren't types,
//   they are identifiers with type ascriptions.