Vijay Dhas Gilbert
Vijay Dhas Gilbert

Reputation: 161

Converting UIFont to CGFont in swift?

Need to pass CGFont for the below function

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

Upvotes: 4

Views: 6256

Answers (2)

CodeBender
CodeBender

Reputation: 36670

Ran into this with Swift 4 and the accepted answer is no longer quite valid. The following took a UIFont and gave me a usable CGFont:

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

Upvotes: 5

Kirsteins
Kirsteins

Reputation: 27345

Here is a small example:

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

var copiedFont = CGFontCreateCopyWithVariations(cgFont, nil)

Upvotes: 10

Related Questions