Swift iOS 13自定义字体下载和安装

Swift iOS 13自定义字体下载和安装,swift,fonts,core-text,ios13,Swift,Fonts,Core Text,Ios13,这不是一个重复的问题 我尽我所能寻找了一切,但没有找到有意义的东西 问题是: 我想下载使用CoreText API的字体,但它在iOS 12中工作,在iOS 13上不可用 2013年,苹果公司发布了一份报告。但自从iOS 13以来,它使用的API似乎是私有的 /* @参数进程块 回调块以指示进度。 返回true继续,返回false取消进程。 在OS X 10.15、iOS 13及更高版本上,在专用串行队列上调用此块。 */ @可用(iOS 6.0,*) public func ctFontDes

这不是一个重复的问题

我尽我所能寻找了一切,但没有找到有意义的东西

问题是:

我想下载使用CoreText API的字体,但它在
iOS 12
中工作,在
iOS 13
上不可用

2013年,苹果公司发布了一份报告。但自从iOS 13以来,它使用的API似乎是私有的

/*
@参数进程块
回调块以指示进度。
返回true继续,返回false取消进程。
在OS X 10.15、iOS 13及更高版本上,在专用串行队列上调用此块。
*/
@可用(iOS 6.0,*)
public func ctFontDescriptorMatchFontDescriptorWithProgressHandler(u描述符:CFArray,uMandatoryAttributes:CFSet?,uProgressBlock:@escaping CTFontDescriptorProgressHandler)->Bool
正如文件所宣布的那样:

在OS X 10.15、iOS 13及更高版本上,在专用串行队列上调用此块

以下是我的字体下载代码:

让fontName=“STXingkai SC灯”
让attributes=[kCTFontNameAttribute:fontName]作为CFDictionary
让fontDescription=CTFontDescriptorCreateWithAttributes(属性)
//创建字体描述符并将其添加到数组中
设descs=[fontDescription]为CFArray
CTFontDescriptorMatchFontDescriptorWithProgressHandler(descs,nil){(state,progressParameter)->Bool-in
让progressValue=(ProgressParameter作为字典)[KCTFontDescriptionFormatChingPercentage]?.doubleValue
开关状态{
case.didbeagin:print(“didbeagin”)
case.didfish:打印(“didfish”)
案例。willBeginDownloading:打印(“willBeginDownloading”)
case.didfishdownloading:
打印(“-->下载完成”)
DispatchQueue.main.async{
self.fontlab.font=UIFont(名称:self.fontName,大小:20)
}
案例.下载:
打印(“下载(progressValue??0.0)”)
DispatchQueue.main.async{
self.progressView.progress=Float(progressValue±0.0)
}
案例.错误:
如果let error=(ProgressParameter作为字典)[KCTFontDescriptionFormatChingerRor]作为?n错误{
打印(错误。说明)
}否则{
打印(“错误消息不可用”)
}
默认值:打印(字符串(反射:状态))
}
返回真值
}
我在iOS 12和Xcode 10上试用了这个API,一切正常,我可以下载一种字体,字体名为

但是,当我在
Xcode 11 11.0版beta 6(11M392q)
macOS Catalina 10.15版beta(19A526h)
上使用此方法时,出现了一些问题,该方法不再有效。我得到了一些结果:

didBegin
__C.CTFontDescriptorMatchingState
didFinish
done
"Error Domain=com.apple.CoreText.CTFontManagerErrorDomain Code=303 \"0 
font descriptors do not have information to specify a font file.\"
UserInfo={NSLocalizedDescription=0 
font descriptors do not have information to specify a font file.

0字体描述符没有指定字体文件的信息

该状态将只有DIDBENGIN和DIDBENGIN,不调用下载

已经宣布,
CTFontManagerRegisterFontDescriptors(fontDescriptors:scope:enabled:registrationHandler:)
可用,并提供了一些关于字体管理的信息。但是会话pdf中的源代码不清楚,我已经尝试过了,得到了另一个错误:

done
[Error Domain=com.apple.CoreText.CTFontManagerErrorDomain Code=306 "The file is not in an allowed location. It must be either in the application’s bundle or an on-demand resource." UserInfo={NSLocalizedDescription=The file is not in an allowed location. It must be either in the application’s bundle or an on-demand resource., CTFontManagerErrorFontURLs=(
    "http://iweslie.com/fonts/HanziPenSC-W3.ttf"
), NSLocalizedFailureReason=Font registration was unsuccessful.}]
done

以下是我的代码引用自WWDC19会话227:

let urlString=”http://iweslie.com/fonts/HanziPenSC-W3.ttf"
让url=url(string:urlString)!作为CFURL
让fontURLArray=[url]作为CFArray
CTFontManagerRegisterFontURLs(fontURLArray.persistent,true){(错误,完成)->Bool-in
如果完成{
打印(“完成”)
}
打印(错误为数组)
返回真值
}
并得到了错误信息:

错误域=com.apple.CoreText.ctfontmanagerrordomain代码=306

“文件不在允许的位置。它必须位于应用程序的捆绑包或按需资源中。”


其他寻求iOS 13解决方案的人 最简单的方法可能是使用Apple捆绑包和“新”的CTFontManagerRegisterFontURLs api。 只需利用Bundle.main.url将所有url收集到一个列表中:

let fontUrl = Bundle.main.url(forResource: fileName, withExtension: "ttf")
然后只需注册您的字体:

CTFontManagerRegisterFontURLs([fontUrl] as CFArray, .persistent, true) { (errors, done) -> Bool in
        if(done) {
            print("Done")
        }
        print(errors as Array)
        return true
    }
如果您使用的是另一种解决方案,并且字体名称有问题,那么可能是xcode出了问题。我在另一个线程中看到的一个提示,用于确定xcode是如何命名字体的:

for family in UIFont.familyNames.sorted() {
        let names = UIFont.fontNames(forFamilyName: family)
        print("Family: \(family) Font names: \(names)")
    }

还记得在项目设置中的签名和功能下启用安装字体功能。

我也面临同样的问题。我终于找到了这个

问题是您使用了错误的字体postscriptname。正确的字体名称是“STXingkaiSC Light”

我已经在xcode 12,iOS 13模拟器上进行了测试


您可以使用“font BooK.app”找到正确的字体postscript名称。

是的,我终于找到了,谢谢您另一个神秘的未记录的Apple API。无法从文件下载,无法从外部url下载。仅捆绑资源或按需资源。唉…要使用这条线路,我需要按需下载资源,对吗?不,那是另一种方式。事实上,当我测试这个时,我第一次试着按需制作。添加资源标签时,文件从Apple捆绑包中消失。因此,请确保删除要使用的文件的资源标记。不过,请确保将文件添加为捆绑资源。您可以在“构建阶段->复制捆绑资源->添加文件”下执行此操作。如果您有很多文件,最好使用按需资源,并从外部服务器获取。安装字体功能是否仅适用于注册的开发人员?苹果网站上没有列出这个主题