下面的代码允许创建具有不同粗细的字体。
func makeFont(weight: CGFloat, size: CGFloat) -> UIFont {
var attributesDict = [String: Any]()
attributesDict["Weight"] = weight
/* Rubik-Light - is a variable font */
let fontDescriptor = UIFontDescriptor(
fontAttributes: [
UIFontDescriptor.AttributeName.name : "Rubik-Light",
kCTFontVariationAttribute as UIFontDescriptor.AttributeName : attributesDict
]
)
return UIFont(descriptor: fontDescriptor, size: size)
}适用于iOS13及以下版本,但不适用于ios 14,有什么解决方案吗?
发布于 2020-12-13 04:34:51
解决了。iOS 14期望属性ID而不是它的名称("Weight")。
因此,attributeDict应该如下所示:
var attributesDict = [NSNumber: Any]()
attributesDict[NSNumber(value: 2003265652)] = weight属性ID的获取方式如下:
let variationAxes = (CTFontCopyVariationAxes(ctFont)! as Array)https://stackoverflow.com/questions/65268747
复制相似问题