extension UIFontDescriptor.AttributeName {
static let nsctFontUIUsage =
UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
}
extension UIFont {
@objc convenience init(myCoder aDecoder: NSCoder) {
if let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as UIFontDescriptor {
if let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String {
var fontName = ""
switch fontAttribute {
case "CTFontRegularUsage":
fontName = AppFontName().regular
case "CTFontEmphasizedUsage", "CTFontBoldUsage":
fontName = AppFontName().bold
case "CTFontObliqueUsage":
fontName = AppFontName().italic
case "CTFontMediumUsage" :
fontName = AppFontName().medium
case "CTFontDemiUsage" :
fontName = AppFontName().semiBold
default:
fontName = AppFontName().regular
}
let savedFontSize = UserDefaults.standard.string(forKey: "font_size") ?? ""
let fontType = FontType.init(rawValue: savedFontSize) ?? .defaultSize
var size = fontDescriptor.pointSize
switch fontType {
case .defaultSize:
size += 0
case .big:
size += 2
case .biggerFont:
size += 4
}
self.init(name: fontName, size: size)!
}
else {
self.init(myCoder: aDecoder)
}
}
else {
self.init(myCoder: aDecoder)
}
}
}
Basically I have been trying to give users the option to increase or decrease the font size according to their needs, while I have been able to successfully provide this functionality but in a lot of places across different pages of the application, fontAttribute is coming out to be nil for some reason and in those places the size does not increase but remains set to default. Why is it coming nil and how do I solve this problem?
P.S: I am very new to the industry, any constructive coding advice is welcome and appreciated.