2014-09-25 50 views
3

需要通過CGFont爲以下功能快速轉換UIFont到CGFont?

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

這個怎麼樣 - http://stackoverflow.com/questions/9205709/how-to-convert-cgfontref-to-uifont – Grimxn 2014-09-25 09:33:39

+0

嗯,他是問對於Swift,但我想這也回答了這個問題:你需要在這裏應用Objective-C。 – 2014-09-25 09:43:13

+0

感謝一噸Grimxn和Peter Branforn – 2014-09-25 11:20:31

回答

6

這裏是一個小例子:

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

var copiedFont = CGFontCreateCopyWithVariations(cgFont, nil) 
+0

明白了。非常感謝 :) – 2014-09-25 11:21:25

1

就遇上了這樣斯威夫特4和接受的答案不再是相當有效的。下面拿了UIFont,給了我一個可用的CGFont

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