iOS自定义字体下载

iOS自定义字体下载,ios,swift,Ios,Swift,iOS允许下载。我已经从中改编了代码,现在面临的一个问题是字体并不是每次都下载,但是每次都会执行ctFontDescriptorMatchFontDescriptorWithProgressHandler。其效果是,任何自定义字体在应用程序启动时都不太可用,直到下载尝试和匹配完成,然后才可用 func fontDownload(name: String) { let size: CGFloat = 17 let f : UIFont! = UIFont(name:name, s

iOS允许下载。我已经从中改编了代码,现在面临的一个问题是字体并不是每次都下载,但是每次都会执行ctFontDescriptorMatchFontDescriptorWithProgressHandler。其效果是,任何自定义字体在应用程序启动时都不太可用,直到下载尝试和匹配完成,然后才可用

func fontDownload(name: String) {
    let size: CGFloat = 17

    let f : UIFont! = UIFont(name:name, size:size)
    if f != nil {
        greekFont = f
        println("Font \(name) already installed")
        return
    }
    println("attempting to download font")
    let desc = UIFontDescriptor(name:name, size:size)
    CTFontDescriptorMatchFontDescriptorsWithProgressHandler(
        [desc], nil, {
            (state:CTFontDescriptorMatchingState, prog:CFDictionary!) -> Bool in
            switch state {
            case .DidBegin:
                println("\(name) matching did begin")
            case .WillBeginDownloading:
                println("\(name) downloading will begin")
            case .Downloading:
                let d = prog as NSDictionary
                let key = kCTFontDescriptorMatchingPercentage
                let cur : AnyObject? = d[key as NSString]
                if let cur = cur as? NSNumber {
                    NSLog("progress: %@%%", cur)
                }
            case .DidFinishDownloading:
                println("\(name) downloading did finish")
            case .DidFailWithError:
                println("\(name) downloading failed")
            case .DidFinish:
                println("\(name) matching did finish")
                dispatch_async(dispatch_get_main_queue(), {
                    let f : UIFont! = UIFont(name:name, size:size)
                    if f != nil {
                        println("Downloaded font: \(name)")
                        self.customFont = f
                    }
                })
            default:break
            }
            return true
    })
}