Commit 590e2b08 by Demid Merzlyakov

Optimization: AppFont main thread impact 5% -> 0.3%.

parent 4c92873d
......@@ -19,24 +19,37 @@ public struct AppFont {
}
public struct SFPro {
private static var fontCache = [UIFont.Weight: [CGFloat: UIFont]]()
static func font(weigth: UIFont.Weight, size: CGFloat) -> UIFont {
if let cached = fontCache[weigth]?[size] {
return cached
}
let descriptor = AppFont.fontDescriptor(size: size, weight: weigth)
let font = UIFont(descriptor: descriptor, size: size)
if fontCache[weigth] == nil {
fontCache[weigth] = [size: font]
}
else {
fontCache[weigth]![size] = font
}
return font
}
static func regular(size: CGFloat) -> UIFont {
let descriptor = AppFont.fontDescriptor(size: size, weight: .regular)
return UIFont(descriptor: descriptor, size: size)
font(weigth: .regular, size: size)
}
static func bold(size: CGFloat) -> UIFont {
let descriptor = AppFont.fontDescriptor(size: size, weight: .bold)
return UIFont(descriptor: descriptor, size: size)
font(weigth: .bold, size: size)
}
static func medium(size:CGFloat) -> UIFont {
let descriptor = AppFont.fontDescriptor(size: size, weight: .medium)
return UIFont(descriptor: descriptor, size: size)
font(weigth: .medium, size: size)
}
static func semibold(size: CGFloat) -> UIFont {
let descriptor = AppFont.fontDescriptor(size: size, weight: .semibold)
return UIFont(descriptor: descriptor, size: size)
font(weigth: .semibold, size: size)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment