在Swift中将UIFont转换为CGFont?

时间:2014-09-25 09:28:48

标签: ios objective-c iphone swift uifont

需要传递CGFont以获取以下功能

CGFontCreateCopyWithVariations(<#font: CGFont!#>, <#variations: CFDictionary!#>))

2 个答案:

答案 0 :(得分:8)

这是一个小例子:

var font = UIFont.systemFontOfSize(15.0)
var fontName = font.fontName as NSString
var cgFont = CGFontCreateWithFontName(fontName);

var copiedFont = CGFontCreateCopyWithVariations(cgFont, nil)

答案 1 :(得分:4)

使用Swift 4进入此区域并且接受的答案不再有效。以下是UIFont并给了我一个可用的CGFont

if let uiFont = UIFont(name: "Helvetica-Bold", size: 36.0),
    let cgFont = CGFont(uiFont.fontName as CFString) {
        // Do stuff        
    }