Converting UIFont to CGFont in swift?

Must be passed CGFont

to the next function

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

      

+3


source to share


2 answers


Here's a small example:



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

var copiedFont = CGFontCreateCopyWithVariations(cgFont, nil)

      

+6


source


Let's get to this with Swift 4 and the accepted answer is no longer valid. The following took UIFont

and gave me a useful one CGFont

:



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

      

+1


source







All Articles