在Swift中将UIFont转换为CGFont?

问题描述 投票:3回答:2

需要通过CGFont以下功能

CGFontCreateCopyWithVariations(<#font: CGFont!#>, <#variations: CFDictionary!#>))
ios objective-c iphone swift uifont
2个回答
8
投票

这是一个小例子:

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

var copiedFont = CGFontCreateCopyWithVariations(cgFont, nil)

4
投票

使用Swift 4进入这个并且接受的答案不再有效。下面拿了一个UIFont并给了我一个可用的CGFont

if let uiFont = UIFont(name: "Helvetica-Bold", size: 36.0),
    let cgFont = CGFont(uiFont.fontName as CFString) {
        // Do stuff        
    }
© www.soinside.com 2019 - 2024. All rights reserved.